F
Filament6mo ago
Max

Header actions on AdminPanel dashboard

Im trying to add some custom buttons for database actions that where prevoiuly on the list users resource in the table header but I want them on the main dashboard as a custom widget. Any help would be awesome!
No description
Solution:
cacheAction is probably a reserved method name since filament caches actions internally. Try renaming it to something else.
Jump to solution
17 Replies
Max
MaxOP6mo ago
No description
Max
MaxOP6mo ago
Current code for custom widget, issue being that its not an actual table so this just breaks when you try to click Really I only need it to look like an action and behave by running custom code
awcodes
awcodes6mo ago
You need to make the actions as methods in the livewire component. You can’t inline them like that in the view. It’s too late to register them in the lifecycle. Widgets are just livewire components. https://filamentphp.com/docs/3.x/actions/adding-an-action-to-a-livewire-component#adding-the-action
Max
MaxOP6mo ago
Any idea why this isnt working?
No description
Max
MaxOP6mo ago
class HeaderActions extends Component implements HasActions, HasForms
{
use InteractsWithActions;
use InteractsWithForms;

public function render()
{
return view('livewire.header-actions');
}

public function deleteAction(): Action
{
return Action::make('delete 2')
->requiresConfirmation()
->action(fn () => dd('delete user'));
}

public function cacheAction(): Action
{
return Action::make('Flush Cache')
->action(function (): void {
$this->flushCache();
})
->label('Flush Cache')
->requiresConfirmation()
->modalHeading('Flush Cache')
->modalDescription('Are you sure you want to flush the cache? This will clear all cached data.')
->modalSubmitActionLabel('Flush Cache');
}

public function flushSessionsAction(): Action
{
return Action::make('Flush Sessions')
->action(function (): void {
$this->flushSessions();
})
->requiresConfirmation()
->label('Flush Sessions')
->modalHeading('Flush Sessions')
->modalDescription('Are you sure you want to flush all sessions?')
->modalSubmitActionLabel('Flush Sessions');
}

protected function flushCache(): void
{
Redis::connection()->flushdb();
Notification::make()
->title('Cache Flushed')
->body('Cache has been flushed successfully.')
->success()
->send();

// Redirect to the dashboard
return redirect()->route('dashboard');
}

protected function flushSessions(): void
{
Redis::connection()->flushdb();
Notification::make()
->title('Sessions Flushed')
->body('Sessions have been flushed successfully.')
->success()
->send();
}
}
class HeaderActions extends Component implements HasActions, HasForms
{
use InteractsWithActions;
use InteractsWithForms;

public function render()
{
return view('livewire.header-actions');
}

public function deleteAction(): Action
{
return Action::make('delete 2')
->requiresConfirmation()
->action(fn () => dd('delete user'));
}

public function cacheAction(): Action
{
return Action::make('Flush Cache')
->action(function (): void {
$this->flushCache();
})
->label('Flush Cache')
->requiresConfirmation()
->modalHeading('Flush Cache')
->modalDescription('Are you sure you want to flush the cache? This will clear all cached data.')
->modalSubmitActionLabel('Flush Cache');
}

public function flushSessionsAction(): Action
{
return Action::make('Flush Sessions')
->action(function (): void {
$this->flushSessions();
})
->requiresConfirmation()
->label('Flush Sessions')
->modalHeading('Flush Sessions')
->modalDescription('Are you sure you want to flush all sessions?')
->modalSubmitActionLabel('Flush Sessions');
}

protected function flushCache(): void
{
Redis::connection()->flushdb();
Notification::make()
->title('Cache Flushed')
->body('Cache has been flushed successfully.')
->success()
->send();

// Redirect to the dashboard
return redirect()->route('dashboard');
}

protected function flushSessions(): void
{
Redis::connection()->flushdb();
Notification::make()
->title('Sessions Flushed')
->body('Sessions have been flushed successfully.')
->success()
->send();
}
}
Max
MaxOP6mo ago
No description
Passenger
Passenger6mo ago
You don't need to call render on the action. Just {{ $this->deleteAction }} is enough. Also. You should name your action to match the method name. So deleteAction() function should return Action::make('delete')->....
Max
MaxOP6mo ago
Ok cool thanks, now im getting This action does not belong to a Livewire component. it works when I have only one action but as soon as I make more than one it breaks complertly
Passenger
Passenger6mo ago
Check that you've imported from the correct name space. use Filament\Actions\Action;
Dennis Koch
Dennis Koch6mo ago
Also make sure your action have the same name as the method.
Max
MaxOP6mo ago
No description
Max
MaxOP6mo ago
No description
No description
Max
MaxOP6mo ago
Yeah they are :/ I did
Max
MaxOP6mo ago
No description
Max
MaxOP6mo ago
There also both rendering cache action
Solution
awcodes
awcodes6mo ago
cacheAction is probably a reserved method name since filament caches actions internally. Try renaming it to something else.
Max
MaxOP6mo ago
That fixed it! Thanks!
Want results from more Discord servers?
Add your server