F
Filament7d 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
Max7d ago
No description
Max
Max7d 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
awcodes7d 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
Max6d ago
Any idea why this isnt working?
No description
Max
Max6d 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
Max6d ago
No description
Passenger
Passenger6d 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
Max4d 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
Passenger4d ago
Check that you've imported from the correct name space. use Filament\Actions\Action;
Dennis Koch
Dennis Koch4d ago
Also make sure your action have the same name as the method.
Max
Max3d ago
No description
Max
Max3d ago
No description
No description
Max
Max3d ago
Yeah they are :/ I did
Max
Max3d ago
No description
Max
Max3d ago
There also both rendering cache action
Solution
awcodes
awcodes3d ago
cacheAction is probably a reserved method name since filament caches actions internally. Try renaming it to something else.
Max
Max3d ago
That fixed it! Thanks!