skip to Main Content

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

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

2

Answers


  1. 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

    Login or Signup to reply.
  2. I think you are looking for this type.

    Model::with(['functionA' => function($query) use ($parameters) {
            $query->where('some_column', $parameters);
        }, 'functionB'])->get(); 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search