Answer the question
In order to leave comments, you need to log in
Which query to choose for the osprey?
In the Product model, you need to get an object that belongs to an authorized user.
Request 1
public function scopeOfUserBySlug($query, $user, $slug)
{
return $query->whereUser_id($user->id)->whereSlug($slug);
}
Product:: ofUserBySlug(Auth::user(), $slug)
public function ofUserBySlug($user, $slug)
{
return $user->products->whereSlug($slug);
}
(new Product)->ofUserBySlug(Auth::user(), $slug)
$product = $user->product($slug
$user->product($slug)
Product:: ofUserBySlug(Auth::user(), $slug)
Answer the question
In order to leave comments, you need to log in
Well, it's written
On line 109 you have this
watch([path.watch.complex], function(event, cb) {
gulp.start('style2:build');
});
The second is not entirely on the subject of Eloquent. It's about magic, and here scopes come in handy. What if more conditions are needed? Well, let's say you leave the call of this method first, and if you need more conditions in the form of methods? That's what scopes were made for, so the first option is definitely better.
Further: Product::
under the hood, it just does (new Product)->
, but besides this, it can do something else that you don’t know about, so I would use Product:: at least for the sake of this and no standardization.
Next: why not do it $user->products()->whereSlug($slug)
? Given the context, I dare to assume that you do not need such a request so often, which means that this is a good solution. You will need an asset - well, add a scope to the Product model and have happiness, like$user->products()->whereSlug($slug)->isActive()
. The last two methods refer to the final Product model, which means that they do not need to be duplicated, but they are relayed in different models (
$user->products(), $page->products(), $something->products()
) is needed, so this is the correct option. $active = true
, you get a mess. Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question