Laravel 拉威尔政策公司
我对laravel不熟悉,现在正在尝试学习政策,因此我创建了以下模型:Laravel 拉威尔政策公司,laravel,laravel-5,Laravel,Laravel 5,我对laravel不熟悉,现在正在尝试学习政策,因此我创建了以下模型: class group extends Model { protected $fillable = [ 'id','book_id', ]; /** * The attributes that should be hidden for arrays. * * @var array */ protected $hidden = [
class group extends Model
{
protected $fillable = [
'id','book_id',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
];
public function users(){
return $this->belongsToMany('BOOK_DONATION\User')->withPivot('last_seen_id');
}
}
以及:
以及:
现在,group_user是用户和组之间多对多关系所需的interdate表的模型
这是我的政策:
class group_userPolicy
{
use HandlesAuthorization;
/**
* Determine whether the user can view the group_user.
*
* @param \BOOK_DONATION\User $user
* @param \BOOK_DONATION\group_user $groupUser
* @return mixed
*/
public function view(User $user, group_user $groupUser)
{
//
return in_array($user->id,$groupUser->user_id);
}
}
以下是与控制器相关的特定编程行:
$grouUser=group_user::where('group_id',$group_id)->pluck('user_id');
if(Auth::user()->cant('joinChat',$grouUser)) return redirect('/sorry');
现在我总是被重定向到“抱歉”页面,但我有权加入聊天室,所以怎么了?$groupUser->user\u id在我看来不像数组。。。如果是,它的命名就很糟糕。如果
groupu\u user
是一个模型,那么就尽量坚持命名约定GroupUser
适用于模型。@Devon您有什么建议?我需要从group_用户模型中获取用户id数组,那么如何实现呢?如果GroupUser
具有users()
关系,您就可以调用If($GroupUser->users()->contains($user->id)){…}
。也许也会发布您的模型代码?@markgroup\u user听起来像一个pivot或中间表。这是非常罕见的需要一个模型。您应该在Laravel文档的belongAsmany部分进一步阅读如何处理用户和组模型之间的多对多关系。
class group_userPolicy
{
use HandlesAuthorization;
/**
* Determine whether the user can view the group_user.
*
* @param \BOOK_DONATION\User $user
* @param \BOOK_DONATION\group_user $groupUser
* @return mixed
*/
public function view(User $user, group_user $groupUser)
{
//
return in_array($user->id,$groupUser->user_id);
}
}
$grouUser=group_user::where('group_id',$group_id)->pluck('user_id');
if(Auth::user()->cant('joinChat',$grouUser)) return redirect('/sorry');