Is it able to use saveRelationships() in action button?

I have a custom page listed created user. I put an action with following info to create a new user with related branches: Action form()
Forms\Components\Section::make()
->schema([
Forms\Components\TextInput::make('user.name')
->label(__('Name'))
->autocomplete(false)
->required(),
]),

Forms\Components\Group::make()
->schema([
Forms\Components\Toggle::make('manage_restaurant_details')
->label(__('Restaurant Details')),
Forms\Components\Toggle::make('manage_branch')
->label(__('Branches')),
Forms\Components\Toggle::make('manage_order')
->label(__('Orders')),
Forms\Components\Toggle::make('manage_qrcode')
->label(__('QR Builder')),
Forms\Components\Toggle::make('manage_membership')
->label(__('Membership')),
Forms\Components\Toggle::make('manage_menu')
->label(__('Menu')),
Forms\Components\Toggle::make('manage_user')
->label(__('Users')),
]),

Forms\Components\Fieldset::make(__('Branches'))
->schema([
Forms\Components\CheckboxList::make('branches')
->hiddenLabel()
->relationship('branches', 'location')
->getOptionLabelFromRecordUsing(fn ($record) => $record->location)
->model(fn ($context) => match ($context) {
'edit' => null,
default => RestaurantUser::class
})
]),
Forms\Components\Section::make()
->schema([
Forms\Components\TextInput::make('user.name')
->label(__('Name'))
->autocomplete(false)
->required(),
]),

Forms\Components\Group::make()
->schema([
Forms\Components\Toggle::make('manage_restaurant_details')
->label(__('Restaurant Details')),
Forms\Components\Toggle::make('manage_branch')
->label(__('Branches')),
Forms\Components\Toggle::make('manage_order')
->label(__('Orders')),
Forms\Components\Toggle::make('manage_qrcode')
->label(__('QR Builder')),
Forms\Components\Toggle::make('manage_membership')
->label(__('Membership')),
Forms\Components\Toggle::make('manage_menu')
->label(__('Menu')),
Forms\Components\Toggle::make('manage_user')
->label(__('Users')),
]),

Forms\Components\Fieldset::make(__('Branches'))
->schema([
Forms\Components\CheckboxList::make('branches')
->hiddenLabel()
->relationship('branches', 'location')
->getOptionLabelFromRecordUsing(fn ($record) => $record->location)
->model(fn ($context) => match ($context) {
'edit' => null,
default => RestaurantUser::class
})
]),
Action action()
->action(function (array $data): void {
$user = User::create($data['user']);
$user->assignRole('restaurant');

$restaurantUser = auth()->user()->currentRestaurant->restaurantUsers()->make($data);
$restaurantUser->user()->associate($user);
$restaurantUser->save();

$this->form->model($restaurantUser)->saveRelationships(); // How to make this line in Action?
})
->form($this->getFormSchema())
->model(RestaurantUser::class)
->action(function (array $data): void {
$user = User::create($data['user']);
$user->assignRole('restaurant');

$restaurantUser = auth()->user()->currentRestaurant->restaurantUsers()->make($data);
$restaurantUser->user()->associate($user);
$restaurantUser->save();

$this->form->model($restaurantUser)->saveRelationships(); // How to make this line in Action?
})
->form($this->getFormSchema())
->model(RestaurantUser::class)
Solution:
Thank you, @Dan Harrin. I forgot about using() in the CreateAction. So, for anyone facing the same situation, this is the correct code (of course, you will modify it according to your code): ```php Actions\CreateAction::make() ->using(function ($data) {...
Jump to solution
3 Replies
MohamedSabil83
MohamedSabil83OP15mo ago
Models: RestaurantUser which belongsTo User and belongsToMany Branch
Dan Harrin
Dan Harrin15mo ago
Have a look at how CreateAction works
Solution
MohamedSabil83
MohamedSabil8315mo ago
Thank you, @Dan Harrin. I forgot about using() in the CreateAction. So, for anyone facing the same situation, this is the correct code (of course, you will modify it according to your code):
Actions\CreateAction::make()
->using(function ($data) {
$user = User::create($data['user']);
$user->assignRole('restaurant');

$restaurantUser = auth()->user()->currentRestaurant->restaurantUsers()->make($data);
$restaurantUser->user()->associate($user);
$restaurantUser->save();

return $restaurantUser;
})
->form($this->getFormSchema())
->model(RestaurantUser::class),
Actions\CreateAction::make()
->using(function ($data) {
$user = User::create($data['user']);
$user->assignRole('restaurant');

$restaurantUser = auth()->user()->currentRestaurant->restaurantUsers()->make($data);
$restaurantUser->user()->associate($user);
$restaurantUser->save();

return $restaurantUser;
})
->form($this->getFormSchema())
->model(RestaurantUser::class),
Docs: https://filamentphp.com/docs/3.x/actions/prebuilt-actions/create#customizing-the-creation-process
Want results from more Discord servers?
Add your server