[英]Why my model can't see spatie permissions trait method on relationship subquery?
I use spatie/laravel-permissions composer package in my laravel projects. 我在laravel项目中使用spatie / laravel-permissions作曲家软件包。 When I run this query:
当我运行此查询时:
$jobs = Job::whereIn('id', $ids)->whereHas('user', function ($query) {
$query->hasRole('company');
})->get();
Return error message 返回错误信息
Call to undefined method Illuminate\\Database\\Eloquent\\Builder::hasRole()
调用未定义的方法Illuminate \\ Database \\ Eloquent \\ Builder :: hasRole()
How I can fix my problem in my case? 我该如何解决我的问题?
The hasRole
-method is not a scope and can't be used on a Builder instance. hasRole
方法不是作用域,不能在Builder实例上使用。
I think you should be able to use the role
-scope in your application. 我认为您应该能够在应用程序中使用
role
-scope 。
$jobs = Job::whereIn('id', $ids)->whereHas('user', function ($q) {
return $q->role('company');
})->get();
Because $user
passed to the function closure is a query builder instance and not an instance of the User
model so where you're declaring the $user
above, make sure to get an instance 由于传递给函数闭包的
$user
是查询生成器实例,而不是User
模型的实例,因此在上面声明$user
,请确保获取实例
$user = User::where(......, ........)->first(); // Without first() it's a query builder
$jobs = Job::whereIn('id', $ids)->whereHas('user', function ($user) {
$user->hasRole('company');
})->get();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.