#Why selectColumn is not working

7 messages · Page 1 of 1 (latest)

slim narwhal
#

Hi i am using the plugin Shield, and i want in mine resource User to be able to change role. I made custom Role model which extends Spatie model, and i added a relationship to the user. Mine problem is that select column is not recognize mine relationship or something like that also i can not add default value for the select column. ```

use Spatie\Permission\Models\Role as SpatieRole;
class Role extends SpatieRole
{
// relationships
public function user() :HasMany
{
return $this->hasMany(User::class);
}
}
User model
public function role()
{
return $this->belongsTo(Role::class);
}
User table public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('name')
->searchable(),
Tables\Columns\TextColumn::make('email')
->searchable(),

            Tables\Columns\SelectColumn::make('role')
                ->searchable(),

            Tables\Columns\TextColumn::make('created_at')
                ->dateTime()
                ->sortable()
                ->toggleable(isToggledHiddenByDefault: true),
            Tables\Columns\TextColumn::make('updated_at')
                ->dateTime()
                ->sortable()
                ->toggleable(isToggledHiddenByDefault: true),
        ])
}```
#

I am not using the default relationship roles because i want to be able to edit the role in table

lilac torrent
#

I think, it should be many to many relationship in to table model_has_roles. Anyway, if you just want a user has only a role, you may do this and set role_id in user table to make relationship. Doing that you should not use Shield, it totally different design.

slim narwhal
#

I already did that, user have in table users role_id, what i am trying to do is when comes to the change i will delete all user roles that he has and add it new

#

But that is not mine problem it is problem why selectColumn is not working, why it does not show the value

brisk vigil
#

If you click edit is it then selectable? Since when viewing it is a readonly

slim narwhal
#

I found the solution it is Tables\Columns\SelectColumn::make('role_id') ->options(function(){ return Role::all()->pluck('name', 'id'); }) ->getStateUsing(function(User $user){ return $user->role_id; }) ->searchable(),