F
Filament•9mo ago
ChesterS

Create action with relation throws error

Sorry for the vague title I have the following action
Actions\Action::make('createUser')
->form([
TextInput::make('email'),
Select::make('roles')
->multiple()
->searchable(false)
->preload()
->options(Roles::options()) // This is an enum
->getOptionLabelFromRecordUsing(fn (Role $record) => Roles::from($record->name)->getLabel())
->relationship('roles', 'name'),
])

->action(function ($record, array $data) {
$record->user()->create($data);
})
Actions\Action::make('createUser')
->form([
TextInput::make('email'),
Select::make('roles')
->multiple()
->searchable(false)
->preload()
->options(Roles::options()) // This is an enum
->getOptionLabelFromRecordUsing(fn (Role $record) => Roles::from($record->name)->getLabel())
->relationship('roles', 'name'),
])

->action(function ($record, array $data) {
$record->user()->create($data);
})
When I click on the button, I get the following error
Filament\Support\Services\RelationshipJoiner::prepareQueryForNoConstraints(): Argument #1 ($relationship) must be of type Illuminate\Database\Eloquent\Relations\Relation, null given, called in /var/www/.../vendor/filament/forms/src/Components/Select.php on line 765
Filament\Support\Services\RelationshipJoiner::prepareQueryForNoConstraints(): Argument #1 ($relationship) must be of type Illuminate\Database\Eloquent\Relations\Relation, null given, called in /var/www/.../vendor/filament/forms/src/Components/Select.php on line 765
This is the exact same form I use in the UserResource and it works when creating a new user, but for some reason doesn't work when doing it outside the resource. I tried adding
->model(User::class)
->model(User::class)
and some other things but nothing worked. What am I doing wrong?
Solution:
Yeap, everything is setup correctly. The form works when used in the UserResource but since I was trying to create the user object from another resource, it would get messed up. Here's the final solution that worked for me ```php Actions\CreateAction::make('createLoginDetails') ->label(__('Create login details')) ->hidden(fn () => $this->getRecord()->user !== null)...
Jump to solution
2 Replies
🤖transistor🤖
Seems to me that the relationship roles does not exist in your model, are you sure that's the correct name?
Solution
ChesterS
ChesterS•9mo ago
Yeap, everything is setup correctly. The form works when used in the UserResource but since I was trying to create the user object from another resource, it would get messed up. Here's the final solution that worked for me
Actions\CreateAction::make('createLoginDetails')
->label(__('Create login details'))
->hidden(fn () => $this->getRecord()->user !== null)
->form(UserResource::getFormFields()->all()) // This gets me the form to create a user
->model(User::class)
->fillForm(fn () => [
'name' => $this->getRecord()->name,
'email' => $this->getRecord()->email,
])
->mutateFormDataUsing(function (array $data) {
$data['contact_id'] = $this->getRecord()->id;

return $data;
}),
Actions\CreateAction::make('createLoginDetails')
->label(__('Create login details'))
->hidden(fn () => $this->getRecord()->user !== null)
->form(UserResource::getFormFields()->all()) // This gets me the form to create a user
->model(User::class)
->fillForm(fn () => [
'name' => $this->getRecord()->name,
'email' => $this->getRecord()->email,
])
->mutateFormDataUsing(function (array $data) {
$data['contact_id'] = $this->getRecord()->id;

return $data;
}),
Essentially, I have a contact record that is linked to a User - users are only used for authentication purposes
Want results from more Discord servers?
Add your server