F
Filament4mo ago
WEBMAS

How to make my custom Prebuilt Actions?

Hello everyone. I want to make my own custom Prebuilt Actions. Here are the default ones https://filamentphp.com/docs/3.x/actions/prebuilt-actions/create I want to make my own, for example WatchAction. I can make a custom action with a form, but I want to use it on different resources without duplicating the full code. How can I do this? I don't see a command to create a custom action.
9 Replies
Lara Zeus
Lara Zeus4mo ago
just create a normal action class and use it anywhere you want you can start by copying any prebuilt ones and place them like in app/filamnet/actions example: https://github.com/lara-zeus/bolt/blob/3.x/src/Filament/Actions/SetResponseStatus.php
WEBMAS
WEBMASOP4mo ago
Thanks. I thought about this solution, but I didn't know exactly where to put the file. So the optimal location is app/Filament/Actions?
Lara Zeus
Lara Zeus4mo ago
the optimal location is wherever you want and however you want to structure your app 🙂 but yea, app/Filament/Actions is a good location
WEBMAS
WEBMASOP4mo ago
How to make a universal Action? So that it is suitable for both tables and pages. If I use Action from tables in pages, then there will be an error "Header actions must be an instance of Filament\Actions\Action, or Filament\Actions\ActionGroup". Is it possible to make a universal one?
Lara Zeus
Lara Zeus4mo ago
this been answered many times before, if you searched "one Action"
WEBMAS
WEBMASOP4mo ago
Can't find "one Action" in discord topics(((
Lara Zeus
Lara Zeus4mo ago
GitHub
Reuse action in both Table and Header · filamentphp filament · Disc...
Hi all, Is there a way to use an action sometimes as a headerAction and sometimes as a table action? I don't want to duplicate my code. I have a ressource list header action: protected function...
Dennis Koch
Dennis Koch4mo ago
It's not really possible currently to make a "universal action".
WEBMAS
WEBMASOP4mo ago
My solution. I don't know how reliable it is, but it works. app/Filament/Actions/WatchAction.php
<?php

namespace App\Filament\Actions;

use Filament\Actions\Action as BasePageAction;
use Filament\Tables\Actions\Action as BaseTableAction;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Str;
use Livewire\Livewire;

class WatchAction
{
public static function make(?string $name = null)
{
if (Str::contains(Route::currentRouteName(), '.index') || Str::contains(Livewire::current()->getName(), '.list-')) {
return WatchTableAction::make($name);
} else {
return WatchPageAction::make($name);
}
}
}

class WatchPageAction extends BasePageAction
{
use HasWatchActionAttributes;
}

class WatchTableAction extends BaseTableAction
{
use HasWatchActionAttributes;
}

trait HasWatchActionAttributes
{
...
}
<?php

namespace App\Filament\Actions;

use Filament\Actions\Action as BasePageAction;
use Filament\Tables\Actions\Action as BaseTableAction;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Str;
use Livewire\Livewire;

class WatchAction
{
public static function make(?string $name = null)
{
if (Str::contains(Route::currentRouteName(), '.index') || Str::contains(Livewire::current()->getName(), '.list-')) {
return WatchTableAction::make($name);
} else {
return WatchPageAction::make($name);
}
}
}

class WatchPageAction extends BasePageAction
{
use HasWatchActionAttributes;
}

class WatchTableAction extends BaseTableAction
{
use HasWatchActionAttributes;
}

trait HasWatchActionAttributes
{
...
}
Want results from more Discord servers?
Add your server