NAVEEN KUMAR
NAVEEN KUMAR

Reputation: 460

Set global where clause when I retrive data using laravel eloquent

I have a table content. So when I retrive anything from that table using laravel eloquent, I need to set a global where clause Content::where('is_playlist', 1)->get();, so that every time I retrieve data from that table, I should automatically filtered with that particular where clause.

Upvotes: 1

Views: 235

Answers (1)

omitobi
omitobi

Reputation: 7334

You can create a scope function in the model to filter that out e.g:

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

This means you can easily call, Content::playlist()->get() and you'll have only playlist content.

I hope this is useful?

Upvotes: 1

Related Questions