Modify query on AttachAction

Hello, I have this code, allowing to attach more products with BelongsToMany Relation:
Tables\Actions\AttachAction::make()
->preloadRecordSelect()
->form(fn(Tables\Actions\AttachAction $action): array => [
$action->getRecordSelect()->required(),
Forms\Components\ColorPicker::make('color')
->required()
])
Tables\Actions\AttachAction::make()
->preloadRecordSelect()
->form(fn(Tables\Actions\AttachAction $action): array => [
$action->getRecordSelect()->required(),
Forms\Components\ColorPicker::make('color')
->required()
])
It works, but I want to modify the query when saving, to add the inverse record in the DB, how do your do that ? Tried $action->getRecordSelect()->saveRelationshipsUsing, but since it doesnt have ->relationship chained it doesnt work. Is there any way to do this, event if its not trough filament? Thanks!
1 Reply
Dimitar Papazov DEV
Solved! After many hours and tests, figure it out. Refrence: https://filamentphp.com/docs/2.x/admin/resources/relation-managers#customizing-the-saving-process Solution:
Tables\Actions\AttachAction::make()
->using(function (Model $record, array $data): Model {
$record->variants()->attach($this->getOwnerRecord()->id, ['color' => $data['color']]);
$this->getOwnerRecord()->variants()->attach($record->id, ['color' => $data['color']]);
return $record;
})
Tables\Actions\AttachAction::make()
->using(function (Model $record, array $data): Model {
$record->variants()->attach($this->getOwnerRecord()->id, ['color' => $data['color']]);
$this->getOwnerRecord()->variants()->attach($record->id, ['color' => $data['color']]);
return $record;
})
Want results from more Discord servers?
Add your server