| 1234567891011121314151617181920212223242526272829303132 | <?phpnamespace App\Scopes;use Illuminate\Database\Eloquent\Builder;use Illuminate\Database\Eloquent\Model;use Illuminate\Database\Eloquent\Scope;//只判断顶级idclass TopDepartmentScope implements Scope{    public $user = [];    public $search = [];    public function __construct($user = [], $search = [])    {        $this->user = $user;        $this->search = $search;    }    public function apply(Builder $builder, Model $model)    {        if(empty($this->search['top_depart_id'])){            //默认进来 只显示自己公司下的 自己权限范围下的部门数据            $top_depart_id = $this->user['depart_top'][0] ?? [];            $top_depart_id = $top_depart_id['depart_id'] ?? 0;        }else{            //查询给的公司下的 自己权限范围下的部门数据            $top_depart_id = $this->search['top_depart_id'];        }        $builder->where('top_depart_id', $top_depart_id);    }}
 |