| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 | <?phpnamespace App\Scopes;use Illuminate\Database\Eloquent\Builder;use Illuminate\Database\Eloquent\Model;use Illuminate\Database\Eloquent\Scope;//只判断顶级id (公司)class 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)    {        //是否所有部门        $is_all_depart = $this->user['is_all_depart'] ?? 0;        //权限范围内的部门        $depart_range = $this->user['depart_range'] ?? [];        //顶级部门        $search_depart_id = $this->search['top_depart_id'] ?? 0;        if(empty($search_depart_id)){            //默认进来 自身顶级公司            $top_depart_id = $this->user['depart_top'][0] ?? [];            $top_depart_id = $top_depart_id['depart_id'] ?? 0;        }else{            //查询 顶级公司            $top_depart_id = $search_depart_id;        }        if($is_all_depart){            //所有部门            if(empty($search_depart_id)){                //全部                $builder->whereIn('top_depart_id', $depart_range);            }else{                //查看某个分社                $builder->where('top_depart_id', $top_depart_id);            }        }else{            //某个分社全部            $builder->where('top_depart_id', $top_depart_id);        }    }}
 |