Skip to content

accessing Spatie User roles in the table #1448

Answered by carbtest
carbtest asked this question in Q&A
Discussion options

You must be logged in to vote

Here is how I accomplished it, not sure if there is a better way

Column::make('Roles', 'id')
    ->format(fn ($value) => implode(", ", DB::table('model_has_roles')
        ->where('model_type', 'App\Models\User')
        ->where('model_id', $value)->join('roles', 'roles.id', '=', 'model_has_roles.role_id')
        ->get()
        ->pluck('name')->toArray())
    ) 
    ->sortable(),

Replies: 4 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@lrljoe
Comment options

lrljoe Oct 25, 2023
Collaborator Sponsor

Answer selected by carbtest
Comment options

You must be logged in to vote
0 replies
Comment options

lrljoe
Oct 26, 2023
Collaborator Sponsor

You must be logged in to vote
1 reply
@carbtest
Comment options

Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants