laravel 本地作用域

喜欢就要表白,这样才能知道你是几号备胎。

 

Scopes 允许您轻松地在模型中复用查询逻辑。要定义 scope,只需在模型方法的前面加上 scope

class User extends Model {
    public function scopePopular($query)
    {
        return $query->where('votes', '>', 100);
    }

    public function scopeWomen($query)
    {
        return $query->whereGender('W');
    }
}

用法:

$users = User::popular()->women()->orderBy('created_at')->get();

 

posted @ 2022-10-18 09:03  方达达  阅读(4)  评论(0编辑  收藏  举报