Populating select drop down with correct Spatie role value
I can display a user's associated Spatie role in a table like this:
->columns([
Tables\Columns\BadgeColumn::make('roles.name'),
])
When I want to edit the user form with a select menu, I can also display the names of the roles like this:
Select::make('role')
->required()
->relationship('role', 'name'),
The problem is, it doesn't seem to pick up the current value of the role in the select dropdown menu..
How do I get it to default to the current value?
If it was my own "roles" model, I'd add a relationship function in there, but in this case it's getting it from the Spatie role model.
I tried to add a custom model to add the relationship, but that didn't work either:
use Spatie\Permission\Models\Role;
class CustomRole extends Role
{
// Additional customizations here
public function customrole()
{
return $this->hasMany(Role::class);
}
}
6 Replies
its because the select should be multiple()
and the relationship name is wrong
a user is associated with multiple roles, not just one
i dont think you need a custom model
Thanks, Dan! That worked! π
I did try multiple() at one stage but I guess the issue was as simple as me not pluralising "roles"..
Much appreciated!
Much appreciated!
my advice would be to check how these packages work internally
in this case, check the HasRoles trait and observe that the relationship method is similar to any that you might write yourself
Thanks, that's great advice. I'm sometimes throwing myself into practical challenges without taking enough time to read up on things properly, in enough detail. I'm new to Laravel as well as Filament and come from a background of doing all this sort of stuff manually in PHP.
fair enough
if you have a strong PHP background, you will have an advantage over lots of other users when it comes to customization
lots of people struggle to find the right abstractions because they dont understand core php concepts
I am also using the spatie role package for my users & I probably have a similar issue as this one. I have a fieldset with a relationship(user) and inside the fieldset there is a select dropdown with roles such as the one above. There issues are two:
1. When I am updating a user there is an error that pops up:
"SQLSTATE[42S22]: Column not found: 1054 Unkown column 'roles' in 'field list'"
2. When I am creating a user after assigning the role and submitting the form, I get the following error exception:
"Array to string conversion"
Earlier it was working fine but since upgrading to v3 it has been an issue. How do I fix this?