Why selectColumn is not working

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);
}
}


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);
}
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),
])
}
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),
])
}
5 Replies
mile4841
mile4841OP4mo ago
I am not using the default relationship roles because i want to be able to edit the role in table
Zen Nitiruj
Zen Nitiruj4mo ago
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.
mile4841
mile4841OP4mo ago
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
toeknee
toeknee3mo ago
If you click edit is it then selectable? Since when viewing it is a readonly
mile4841
mile4841OP3mo ago
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(),
Tables\Columns\SelectColumn::make('role_id')
->options(function(){
return Role::all()->pluck('name', 'id');
})
->getStateUsing(function(User $user){
return $user->role_id;
})
->searchable(),
I just have to find method that is similar to beforeUpdate
Want results from more Discord servers?
Add your server