F
Filament7mo ago
ericmp

Redirect after bulk action

is this how u would redirect after a bulk action is completed?
Tables\Actions\BulkAction::make('create_playlist')
->action(function (Collection $records) {
$playlist = Playlist::create([
'name' => null,
]);

$playlist->songs()->attach($records);

return to_route('filament.admin.resources.playlists.edit', $playlist);
})
->icon('heroicon-o-plus')
->color(Color::Indigo)
,
Tables\Actions\BulkAction::make('create_playlist')
->action(function (Collection $records) {
$playlist = Playlist::create([
'name' => null,
]);

$playlist->songs()->attach($records);

return to_route('filament.admin.resources.playlists.edit', $playlist);
})
->icon('heroicon-o-plus')
->color(Color::Indigo)
,
i tried to use $this->to_route() but im not in an object context i prefer $this->to_route() since is the livewire one. the return to_route() is the laravel one. and the diference is that the filament notifications/livewire stuff glitches a little bit, just a little, before redirecting. instead, the livewire one doesnt do it ^^
15 Replies
ericmp
ericmp7mo ago
anyone?
awcodes
awcodes7mo ago
Inject $livewire into the ->action() callback then you can call it.
$livewire->to_route()
$livewire->to_route()
ericmp
ericmp7mo ago
how can inject it? i get this:
No description
ericmp
ericmp7mo ago
im also unable to get the name, which i have filled in the form :/ i cant inject $get
ericmp
ericmp7mo ago
but isnt that what i have done? i missed the type Component? still get same error though maybe for bulk actions is different?
awcodes
awcodes7mo ago
In livewire v3 it’s ->redirectRoute('profile')
ericmp
ericmp7mo ago
oh u r right omg thanks
awcodes
awcodes7mo ago
There is no to_route() method as the error is telling you.
ericmp
ericmp7mo ago
yeah 🥲
marcosmarcolin
marcosmarcolin5mo ago
@ericmp Hi, did you manage to solve it? I am in the same.
ericmp
ericmp5mo ago
hi, i did it this way:
Tables\Actions\BulkAction::make('create_playlist')
->form([
Forms\Components\TextInput::make('name')
->autofocus()
->required()
->unique(Playlist::class)
,
])
->action(function (Collection $records, Component $livewire, array $data) {
$playlist = Playlist::create([
'name' => $data['name'],
]);

$playlist->songs()->attach($records);

$livewire->redirectRoute('filament.admin.resources.playlists.edit', $playlist);
})
->icon('heroicon-o-plus')
->color(Color::Indigo)
,
Tables\Actions\BulkAction::make('create_playlist')
->form([
Forms\Components\TextInput::make('name')
->autofocus()
->required()
->unique(Playlist::class)
,
])
->action(function (Collection $records, Component $livewire, array $data) {
$playlist = Playlist::create([
'name' => $data['name'],
]);

$playlist->songs()->attach($records);

$livewire->redirectRoute('filament.admin.resources.playlists.edit', $playlist);
})
->icon('heroicon-o-plus')
->color(Color::Indigo)
,
marcosmarcolin
marcosmarcolin5mo ago
Hi @ericmp, It was the solution I found too, thanks for your time. https://discord.com/channels/883083792112300104/1209647616023863308
ericmp
ericmp5mo ago
nicee. but what is the link u share below? is another problem u have or is related?
marcosmarcolin
marcosmarcolin5mo ago
It's a similar problem to what you were experiencing, I ended up opening the topic before finding this one.