J
J
jazzus2019-01-26 05:39:08
JavaScript
jazzus, 2019-01-26 05:39:08

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);
    }

Called Product:: ofUserBySlug(Auth::user(), $slug)
Request 2
public function ofUserBySlug($user, $slug)
    {
        return $user->products->whereSlug($slug);
    }

Called What to choose and why? Ps Now in the whole application ) But since. already made several mistakes (received not from an authorized user and it was imperceptible) decided to put this request into the model. Is that the right thing to do? Or leave everywhere? Or remove $user->product($slug) into a trait and call A trait to connect to the model. I want it to be immediately clear what the request is. Plus, suddenly you will need to add something to this builder, some kind of active, true - and then you will need to change it throughout the application. In general, it's probably better to take it out. There will be many replacements (for this example, other objects). How would you do? (product($slug) is 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

2 answer(s)
N
Nikolay Talanov, 2016-06-02
@Sashjkeee

Well, it's written
On line 109 you have this

watch([path.watch.complex], function(event, cb) {
        gulp.start('style2:build');
     });

At the same time, there is naturally no Complex inside path.watch, which is what this error tells you about. Moreover, this block of code is then repeated again.

A
Alex Wells, 2019-01-26
@jazzus

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.
Moreover, the Product should not be responsible for the user's methods (as in your second option), and if you cram a few more from different models there, with all sorts $active = true, you get a mess.
TL;DR: Stick to what Eloquent suggests and you'll be fine.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question