lundi 27 février 2017

Query if a relationship exists with conditions in Laravel

I have a model Foo that contains a hasMany relationship to Bar.

I have a query similar to the following:

$r = Foo::with(['bar' => function($query) {
    $query->where('someProp', '=', 10);
})->get()

However, I want to only return the Foo object if item has a Bar object that satisfies the query.

I'm aware that you can do something like this:

$r = Foo::has('bar')
    ->with(['bar' => function($query) {
        $query->where('someProp', '=', 10);
    })->get();

But that checks if any bar items exists. Not if a bar item exists with someProp = 10

How can I do this?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2lqtI0t
via IFTTT

Aucun commentaire:

Enregistrer un commentaire