netdjw
netdjw

Reputation: 6007

How to use parameters in Laravel Eloquent Eager Loading functions?

I need to run a select on multiple database tables using Laravel 5.6 and I get parameters from the post $request.

I want to do something like this, but this is wrong:

Casefile::with([
    'client',
    'loan' => function($query) {
         $query->where('bank_name','REGEXP',$request->bank_name)
    },
    'section'
])->where($find)->get();

In this example the $request will be an undefined value, but earlier it is defined and has value.

What is the correct way to do this query?

Upvotes: 1

Views: 1272

Answers (1)

Jonas Staudenmeir
Jonas Staudenmeir

Reputation: 25936

You have to make $request available inside the closure:

function($query) use($request) {

Upvotes: 3

Related Questions