Edit Form multiselect with Spatie laravel-Permission - Struggling to get it to work

Continuing on from Chat, where I can't get the default relationship method to work on the select I've got this at least opening up with the right roles already assigned - but searching for and selecting any others the names change to id numbers and clicking submit does not save any changes.
class Edit extends Component implements Forms\Contracts\HasForms
{
use Forms\Concerns\InteractsWithForms;

public User $user;

public $name = '';
public $slug = '';
public $email = '';
public $job_title = '';
public $active;
public $roles = [];

public function mount(): void
{
$this->form->fill([
'name' => $this->user->name,
'slug' => $this->user->slug,
'email' => $this->user->email,
'roles' => $this->user->roles->pluck('name')->toArray(),
]);
}
class Edit extends Component implements Forms\Contracts\HasForms
{
use Forms\Concerns\InteractsWithForms;

public User $user;

public $name = '';
public $slug = '';
public $email = '';
public $job_title = '';
public $active;
public $roles = [];

public function mount(): void
{
$this->form->fill([
'name' => $this->user->name,
'slug' => $this->user->slug,
'email' => $this->user->email,
'roles' => $this->user->roles->pluck('name')->toArray(),
]);
}
Forms\Components\Select::make('roles')
->searchable()
->multiple()
->getSearchResultsUsing(fn (string $query) => Role::where('name', 'like', "%{$query}%")->limit(50)->pluck('name', 'id'))
->getOptionLabelUsing(fn ($value): ?string => Role::find($value)?->name)
->label('Roles assigned')
->helperText('Select roles to assign to this user.'),
Forms\Components\Select::make('roles')
->searchable()
->multiple()
->getSearchResultsUsing(fn (string $query) => Role::where('name', 'like', "%{$query}%")->limit(50)->pluck('name', 'id'))
->getOptionLabelUsing(fn ($value): ?string => Role::find($value)?->name)
->label('Roles assigned')
->helperText('Select roles to assign to this user.'),
public function submit()
{
$this->user->update(
$this->form->getState(),
);

Notification::make()
->title('Updated successfully')
->body($this->user->name.' successfully updated in database.')
->success()
->send();

return redirect()->route('users.show', $this->user);
}
public function submit()
{
$this->user->update(
$this->form->getState(),
);

Notification::make()
->title('Updated successfully')
->body($this->user->name.' successfully updated in database.')
->success()
->send();

return redirect()->route('users.show', $this->user);
}
This
4 Replies
Dan Harrin
Dan Harrin2y ago
you need to remove the 'roles' array key from the fill() and make sure you define getFormModel() to return $this->user you should try ->relationship('roles', 'name') instead of ->getSearchResultsUsing() and ->getOptionLabelUsing()
cigoler
cigolerOP2y ago
protected function getFormModel(): string
{
return $this->user;
}

public function mount(): void
{
$this->form->fill([
'name' => $this->user->name,
'slug' => $this->user->slug,
'email' => $this->user->email,
]);
}
protected function getFormModel(): string
{
return $this->user;
}

public function mount(): void
{
$this->form->fill([
'name' => $this->user->name,
'slug' => $this->user->slug,
'email' => $this->user->email,
]);
}
And using
->relationship('roles', 'name')
->relationship('roles', 'name')
is just producing that
Illuminate \ Contracts \ Container \ BindingResolutionException target class not found
Illuminate \ Contracts \ Container \ BindingResolutionException target class not found
error from earlier. I'm guessing I'm doing something fundamentally wrong or it's specific to the spatie package.
cigoler
cigolerOP2y ago
Works on the table, for example using roles.name, I'll keep fiddling.
cigoler
cigolerOP2y ago
I think this might be the problem:
protected function getFormModel(): string
protected function getFormModel(): string
should be User at the end, not sure where I got string from.
Want results from more Discord servers?
Add your server