[英]laravel show multiple data in datatables
I work with laravel spatie permission package and need to show user role for each user in datatables lists.我使用 laravel spatie 权限包,需要在数据表列表中显示每个用户的用户角色。 now I add datatables
addColumn
method:现在我添加数据表
addColumn
方法:
$items = Admin::orderBy('id','DESC');
return DataTables::of($items)
->addColumn('role',function(Admin $admin){
foreach($admin->getRoleNames() as $v){
return $v;
}
})
->toJson();
this code work and show role name for each user but If user have a two role name(multiple roles) My code not work and show only first role name.此代码有效并为每个用户显示角色名称,但如果用户有两个角色名称(多个角色),我的代码不起作用并且仅显示第一个角色名称。 ie: for user
test
I have two roles name: moderator
and editor
But i see only moderator.即:对于用户
test
我有两个角色名称: moderator
和editor
但我只看到主持人。
How to can i show multiple roles name for each user in datatables list?!如何在数据表列表中为每个用户显示多个角色名称?!
public function roles()
{
return $this->hasMany('App\Role');
}
public function user()
{
return $this->belongsTo('App\User');
}
I think that you should add these methods to your user
Model and the second one to your role
Model我认为您应该将这些方法添加到您的
user
模型中,并将第二个方法添加到您的role
模型中
I found this and work true:我发现了这一点并且工作正常:
return DataTables::of($items)
->addColumn('roles',function(Admin $admin){
return $admin->roles->pluck('name')->toArray();
})
->toJson();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.