I pulled the global scope out of the Model and created a Scope file in the /app.
(我从模型中拉出了全局范围,并在/ app中创建了一个范围文件。)
as follows: (如下:)
<?php
namespace AppScopes;
use IlluminateDatabaseEloquentScope;
use IlluminateDatabaseEloquentModel;
use IlluminateDatabaseEloquentBuilder;
class UserScope implements Scope
{
public function apply(Builder $builder, Model $model)
{
$authUser = auth()->user()->sites()->select('id')->get()->pluck('id')->toArray();
$builder->whereHas('sites', function($q) use($authUser) {
$q->whereIn('id', $authUser);
});
}
}
The problem I was having was using where instead of whereHas and whereIn as per above.
(我遇到的问题是使用where代替上述的whereHas和whereIn。)
Then in the User Model include the following:
(然后在用户模型中包括以下内容:)
use AppScopesUserScope;
protected static function boot()
{
parent::boot();
static::addGlobalScope(new UserScope);
}
Spartieboy your answer was correct for returning the users site id as an array.
(Spartieboy,您的答案对于以数组形式返回用户站点ID是正确的。)
Thanks for that. (感谢那。)
The only problem is that when I log out of the app and log back in I get the following error:
(唯一的问题是,当我注销该应用程序并重新登录时,出现以下错误:)
"Call to a member function sites() on null" - hitting the following code:
(“在空函数上调用成员函数sites()”-击中以下代码:)
$authUser = auth()->user()->sites()->select('id')->get()->pluck('id')->toArray();
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…