更新时间:2023-12-02 15:27:10
我在GitHub上的Laravel Framework问题跟踪器上发布了一个类似的问题,作为可能的错误(不确定是否会发生这种现象),并在那里找到了答案.
I posted a similar question as a possible bug (was not sure whether this behavior was something expected) on Laravel Framework issue tracker on GitHub and found the answer there.
结果是,您无法在全局查询范围构造函数中访问经过身份验证的用户,但可以在apply()方法中对其进行访问.
Turns out, you cannot access the authenticated user in the global query scope constructor, but it is accessible in the apply() method.
所以代替:
public function __construct() {
$this->region_id = Auth::user()->region_id;
}
public function apply(Builder $builder, Model $model)
{
$builder->where('region_id', $this->region_id);
}
我必须做:
public function apply(Builder $builder, Model $model)
{
$region_id = Auth::user()->region_id;
$builder->where('region_id', $region_id);
}
在GitHub 此处上链接到该问题.
Link to the issue on GitHub here.