List From a Model - Form and Save from another Model with Relationship

I have a table with Products (I have all the basic info) and another table ExtraFieldProduct (extra fields for the products). What I would need is to list the products in the table, but when I click on a record send the id to the ExtraFieldProduct Form, set the id to product_id and then fill in the form to create or edit. Thanks!!!
4 Replies
Patrick Boivin
Just a thought : It may be easier to automatically create the related ExtraFieldProduct when a Product is created... Then you can add an action in the Products table that takes you to edit the extra fields.
marianov24
marianov24OP2y ago
I got the product information from a webhook, but some times we need add extra fields because of this i make two tables
Patrick Boivin
Makes sense, I think I understand. Here's what I have in mind... maybe you can adapt it a bit more for your needs :
Tables\Actions\Action::make('editExtraField')
->action(function ($record) {
$extraField = $record->extraField ?? ExtraFieldProduct::create(['product_id' => $record->id]);

return redirect(ExtraFieldProductResource::getUrl('edit', ['record' => $extraField->id]));
});
Tables\Actions\Action::make('editExtraField')
->action(function ($record) {
$extraField = $record->extraField ?? ExtraFieldProduct::create(['product_id' => $record->id]);

return redirect(ExtraFieldProductResource::getUrl('edit', ['record' => $extraField->id]));
});
Check out the docs on listing actions for more examples : https://filamentphp.com/docs/2.x/admin/resources/listing-records#actions
marianov24
marianov24OP2y ago
thanks, i going to read.
Want results from more Discord servers?
Add your server