定义:
class User extends Model
{
public function scopePopular($query)
{
return $query->where('votes', '>', 100);
}
public function scopeActive($query)
{
return $query->where('active', 1);
}
public function scopeOfType($query, $type)
{
return $query->where('type', $type);
}
}
使用:
$users = App\User::popular()->active()->ofType('admin')->orderBy('created_at')->get();
$users = App\User::popular()->orWhere(function (Builder $query) {
$query->active();
})->get();
# 或者
$users = App\User::popular()->orWhere->active()->get();
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。