W
W
WebDev2016-02-09 10:56:51
Laravel
WebDev, 2016-02-09 10:56:51

Laravel fetch conditions with?

Hello everyone, there is such a structure: Categories -> Parameters -> Parameter values
​​I get all this in one request:
Category::with('params.values')->get();
How can I add conditions specifically for params?
The documentation has an example with conditions:

$users = User::with(['posts' => function($query)
{
    $query->where('title', 'like', '%первое%');
}])->get();

But here with with one argument, and I have two.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrzej Wielski, 2016-02-09
@kirill-93

$users = User::with(['posts' => function($query)
{
    $query->where('title', 'like', '%первое%');
},
'foo',
'bar' => function($query){
    // bar query
}])->get();

Pass as array elements.
I think whereHas is also suitable for you:
Category::with('params.values')->whereHas('params', function($query){
   $query->where('something', 'foobar');
})->get();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question