Answer the question
In order to leave comments, you need to log in
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();
$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
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 questionAsk a Question
731 491 924 answers to any question