dimanche 31 juillet 2016

Filtering models by eager loaded data, at the database level

In the laravel docs they discuss eager loading related data to solve the N+1 query problem. I would like to filter my data based on the data from these relationships, without having to iterate through the results (that is, I would like this done at query time)

An example used in the docs is as follows:

$books = App\Book::with('author.contacts')->get();

If I wanted to filter these books to only include those who's author lives in the zip code 12345, how would I do that?

Neither of the following queries work for me:

$books = App\Book::with('author.contacts')->where('zip', 12345)->get();
$books = App\Book::with('author.contacts')->where('author.contacts.zip', 12345)->get();

Is there a simple way to do this in Eloquent?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire