jeudi 24 septembre 2015

Laravel 5.1 global scopes... Where should I go?

Well, I've started with Laravel just a few weeks ago, so sorry if I'm repeating something obvious but... here's the thing:

I've got a couple of query scopes in a Photo model:

public function scopeSkipFirst($query) 
{
    return $query->where('id', '>', 1);
}

public function scopeSearch($query, $search) 
{
    return $query->where('title', 'LIKE', "%$search%");
}

Now, I want the first one to be executed everytime I make an Eloquent query by that model, like in Photo::all(); and I want the second query scope to be available to any other model.

What's the best practice way to do this? Are both scenarios "global scopes"? I've been reading a few posts (like this one), but I have no clear ideas about which documentation should I refer (Laravel's 4.2 # Global scopes section; 5.1 Eloquent's # Events; ¿?).



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire