Action on a resource that creates another resource?
Hello !! Is it possible to create an action on a resource that creates another resource?
For example, I have the invoice resorces page ,
I want to add a button that passes the invoice data to the resource Order page + some custom data.
For now the only thing I could achieve is a redirect action to create a new order
->action(fn() => redirect()->route('filament.admin.resources.orders.create')),
tnxx !!
Solution:Jump to solution
I achieve what I wanted, directly inserting the data in the DB
->action(function($record) {
DB::table('orders')->insert(
array('client_id' => $record->client_id, 'obra' => 'TEST', 'description' => $record->invoice_number, 'article_id' => 2, 'created_by' => auth()->user()->id)
);...
3 Replies
These may help with passing the current record ID or other data to the action:
https://filamentphp.com/docs/3.x/tables/actions#injecting-the-current-eloquent-record
https://filamentphp.com/docs/3.x/actions/advanced
Thanks for the answer, I'm going to investigate, for now I was able to send the data through the url and by request insert it into the forms, there must be a way to directly create the record without this step
In the InvoiceResource i create a action
Tables\Actions\Action::make('OrderCreate')
->action(fn ($record) => redirect()->route('filament.admin.resources.orders.create',['invoice_number' => $record->invoice_number, 'client_id' => $record->client_id,'obra' => $record->obra]))
And in the OrderResource
Forms\Components\TextInput::make('obra')
->default(function(Request $request) {
return $request->input('invoice_number');
})
With this I fill the field
Solution
I achieve what I wanted, directly inserting the data in the DB
->action(function($record) {
DB::table('orders')->insert(
array('client_id' => $record->client_id, 'obra' => 'TEST', 'description' => $record->invoice_number, 'article_id' => 2, 'created_by' => auth()->user()->id)
);
})