Forwarding to an url after action is finished

Is it possible to redirect to for example the index page after an action has finished?
Actions\Action::make('send')
->visible(function (Order $order) {
return ! $order->sent && ! $order->supplier->send_automatically;
} )
->icon('heroicon-o-paper-airplane')
->label('Verzenden')
->tooltip('Order verzenden')
->requiresConfirmation()
->modalHeading('Order verzenden')
->modalDescription('Weet je zeker dat je deze order wilt verzenden?')
->modalSubmitActionLabel('Verzenden')
->action(function (Order $order) {
SendOrder::dispatch($order);

// @todo updating the order is already done in the job so this is unnecessary,
// but I don't know how to refresh the table without it
$this->refreshFormData(
['items']
);
})
Actions\Action::make('send')
->visible(function (Order $order) {
return ! $order->sent && ! $order->supplier->send_automatically;
} )
->icon('heroicon-o-paper-airplane')
->label('Verzenden')
->tooltip('Order verzenden')
->requiresConfirmation()
->modalHeading('Order verzenden')
->modalDescription('Weet je zeker dat je deze order wilt verzenden?')
->modalSubmitActionLabel('Verzenden')
->action(function (Order $order) {
SendOrder::dispatch($order);

// @todo updating the order is already done in the job so this is unnecessary,
// but I don't know how to refresh the table without it
$this->refreshFormData(
['items']
);
})
I wanted to refresh the whole form (this way some fields will become not editable). The above setup doesn't work. So I thought maybe it's possible to redirect to the index page after the order has been sent. Any suggestions?
18 Replies
JJSanders
JJSandersOP11mo ago
Thanks for your reply. I tried this:
->successRedirectUrl(route('orders.list'))
->successRedirectUrl(route('orders.list'))
but I got: Route [orders.list] not defined. I have this list of routes defined in my orderResource:
return [
'index' => Pages\ListOrders::route('/'),
'create' => Pages\CreateOrder::route('/create'),
'edit' => Pages\EditOrder::route('/{record}/edit'),
];
return [
'index' => Pages\ListOrders::route('/'),
'create' => Pages\CreateOrder::route('/create'),
'edit' => Pages\EditOrder::route('/{record}/edit'),
];
But I get that the route is not defined. I have tried:
->successRedirectUrl(route('index'))
->successRedirectUrl(route('index'))
and
->successRedirectUrl(route('orders.index'))
->successRedirectUrl(route('orders.index'))
As well.
BlackShadow
BlackShadow11mo ago
Try OrderResource::getUrl()
JJSanders
JJSandersOP11mo ago
Thanks that returns the right url. But the redirect doesn't work.
->successRedirectUrl(OrderResource::getUrl('index'))
->successRedirectUrl(OrderResource::getUrl('index'))
Nothing happends it just stays on the same page.
BlackShadow
BlackShadow11mo ago
Probably because its not getting called You probably need to redirect yourself in the action Something like:
redirect()->route(OrderResource::getUrl('index'))
redirect()->route(OrderResource::getUrl('index'))
nostrodamned
nostrodamned11mo ago
i dont know if this helps but did this to redirect after a confirmation action https://discord.com/channels/883083792112300104/1202209539848413204
JJSanders
JJSandersOP11mo ago
So when I do this
->action(function (Order $order) {
SendOrder::dispatch($order);
Notification::make()
->success()
->title('Order verwerkt')
->body('De order is succesvol verwerkt.');
redirect()->route(OrderResource::getUrl('index'));
})
->action(function (Order $order) {
SendOrder::dispatch($order);
Notification::make()
->success()
->title('Order verwerkt')
->body('De order is succesvol verwerkt.');
redirect()->route(OrderResource::getUrl('index'));
})
I get a big error that the route is not defined. But when I copy / paste the route from the error and add it in my url bar it just works
BlackShadow
BlackShadow11mo ago
Did you import the OrderResource? And check the routes in the OrderResource.
JJSanders
JJSandersOP11mo ago
Yes:
return [
'index' => Pages\ListOrders::route('/'),
'create' => Pages\CreateOrder::route('/create'),
'edit' => Pages\EditOrder::route('/{record}/edit'),
];
return [
'index' => Pages\ListOrders::route('/'),
'create' => Pages\CreateOrder::route('/create'),
'edit' => Pages\EditOrder::route('/{record}/edit'),
];
These are my routes
nostrodamned
nostrodamned11mo ago
->action(function (Order $order){ SendOrder::dispatch($order); Notification::make() ->success() ->title('Order verwerkt'); return redirect(OrderResource::getUrl('index')); })
BlackShadow
BlackShadow11mo ago
Yea, so App\Filament\Resources\OrderResource::getUrl() should work. Also, you don't have to use 'index' since thats default on getUrl().
JJSanders
JJSandersOP11mo ago
->action(function (Order $order) {
SendOrder::dispatch($order);
Notification::make()
->success()
->title('Order verwerkt')
->body('De order is succesvol verwerkt.');
return redirect()->route(OrderResource::getUrl());

}),
->action(function (Order $order) {
SendOrder::dispatch($order);
Notification::make()
->success()
->title('Order verwerkt')
->body('De order is succesvol verwerkt.');
return redirect()->route(OrderResource::getUrl());

}),
Same error
BlackShadow
BlackShadow11mo ago
What is the error.
nostrodamned
nostrodamned11mo ago
what is errro? lol sorry @CodeWithDennis !
JJSanders
JJSandersOP11mo ago
I had tho remove the redirect() so then it worked. Question remains: what is this method for: successRedirectUrl(route('index')) Thanks for your help
BlackShadow
BlackShadow11mo ago
Thats odd.. successRedirectUrl is for saving / creating i think.
nostrodamned
nostrodamned11mo ago
I hit the same issue befor with notifications etc happening before the redirect - thats how i got around it successRedirect is just on Save/Create as CodewithDennis says
Want results from more Discord servers?
Add your server