Laravel 8 应用全局作用域
2021-07-19 11:32 更新
要将全局作用域分配给模型,需要重写模型的 booted 方法并使用 addGlobalScope 方法:
<?php
namespace App\Models;
use App\Scopes\AgeScope;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* 模型的 "booted" 方法
*
* @return void
*/
protected static function booted()
{
static::addGlobalScope(new AgeScope);
}
} 添加作用域后,对 User::all() 的查询会生成以下 SQL 查询语句:
select * from `users` where `age` > 200 以上内容是否对您有帮助:

免费 AI IDE


更多建议: