J
J
jazzus2018-12-27 15:49:21
Laravel
jazzus, 2018-12-27 15:49:21

Which query is better to use (and why)?

Request 1

$file=Auth::user()
                              ->products()
                              ->where('slug',$product_slug)
                              ->firstOrFail()
                              ->files()
                              ->where('slug', $file_slug)
                              ->firstOrFail();

Request 2
$file=File::where('slug', $file_slug)
                               ->whereHas('product', function ($query) use ($product_slug) {
                         $query->where('products.user_id', '=', Auth::id());
                         $query->where('products.slug', '=', $product_slug);
                     })
                               ->firstOrFail();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Kovalchuk, 2018-12-27
@jazzus

of the 2 indicated, I am for 1 because whereHas is very slow, and there is no saving in the number of queries in option 2, but as for me it is better to use the hasManyThrough connection here

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question