Home > Back-end >  How To Pass Parameters In String Array Of Functions In With() Laravel
How To Pass Parameters In String Array Of Functions In With() Laravel

Time:11-09

I Try To Pass Parameter To With() Function For Model:

Model::with(['functionA($parameters)', 'functionB'])

CodePudding user response:

Unfortunately, you can't use 'with' for normal function, it's used for eloquent's relationships. for your case you can proceed like this:

$model = new Model(); // or $model = Model::where(anyConditionYouWant)->first();
$model->functionA($parameters);

you can use your functions any time you want without declare them in 'with' sentence

CodePudding user response:

I think you are looking for this type.

Model::with(['functionA' => function($query) use ($parameters) {
        $query->where('some_column', $parameters);
    }, 'functionB'])->get(); 
  • Related