Query scopes
Local scopes package a reusable query fragment in a model method prefixed with scope:
class Post extends FModel
{
public function scopePopular($query)
{
return $query->where(['likes', '>', 100]);
}
public function scopeOfStatus($query, string $status)
{
return $query->where(['status', $status]);
}
}
Call the scope without its prefix:
$posts = Post::popular()->orderByDesc('likes')->get();
$posts = Post::ofStatus('published')->get();
A scope receives a new Firestore query builder. Return that builder so callers can continue chaining supported query methods.
Global Eloquent scopes are not implemented in version 4.0.