How to get role name #239
-
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Hi Luis, I am not sure if this is the best approach, I believe it's not enough for filtering by the relationship for example. I have never used Spatie Roles, but I had a go with it: public function datasource(): ?Builder
{
return User::query()->with('roles');
} It's unclear to me if a user can have multiple roles. A So, I follow this relationship, plucking the Roles names and joining them together by ",". public function addColumns(): ?PowerGridEloquent
{
return PowerGrid::eloquent()
->addColumn('id')
->addColumn('last_name')
->addColumn('role_name', function (User $user) {
return $user->roles
->pluck('name')
->implode(',');
});
} And I get: @luanfreitasdev a better idea would be to Join table? https://livewire-powergrid.com/#/table/datasource?id=join-tables |
Beta Was this translation helpful? Give feedback.
-
hello, thank you very much for your answer, I tried it and it worked |
Beta Was this translation helpful? Give feedback.
Hi Luis,
I am not sure if this is the best approach, I believe it's not enough for filtering by the relationship for example.
I have never used Spatie Roles, but I had a go with it:
It's unclear to me if a user can have multiple roles.
A
dd($user->roles)
shows me:So, I follow this relationship, plucking the Roles names and joining them together by ",".