How to Create an Action class that can be used on both Table and Form

Hi, I have a pretty big action with form and form handling logic. I need to extract it to a class which can be used on both the Resource Table and the Resource Edit form.
2 Replies
Mansoor Khan
Mansoor KhanOP12mo ago
Ok I just created a trait which i use in both actions. I dont get the ide completion but it works.. May be someone has better approach.. Here is an Example: app/Filament/Actions/Form/ReceiveInvoicePayment.php
<?php

namespace App\Filament\Actions\Form;

use App\Filament\Actions\Concerns\ReceivesInvoicePayment;
use Filament\Actions\Action;

class ReceiveInvoicePayment extends Action
{
use ReceivesInvoicePayment;
}
<?php

namespace App\Filament\Actions\Form;

use App\Filament\Actions\Concerns\ReceivesInvoicePayment;
use Filament\Actions\Action;

class ReceiveInvoicePayment extends Action
{
use ReceivesInvoicePayment;
}
app/Filament/Actions/Table/ReceiveInvoicePayment.php
<?php

namespace App\Filament\Actions\Table;

use App\Filament\Actions\Concerns\ReceivesInvoicePayment;
use Filament\Tables\Actions\Action;

class ReceiveInvoicePayment extends Action
{
use ReceivesInvoicePayment;
}
<?php

namespace App\Filament\Actions\Table;

use App\Filament\Actions\Concerns\ReceivesInvoicePayment;
use Filament\Tables\Actions\Action;

class ReceiveInvoicePayment extends Action
{
use ReceivesInvoicePayment;
}
The trait: app/Filament/Actions/Concerns/ReceivesInvoicePayment.php
<?php

namespace App\Filament\Actions\Concerns;

use App\Models\Invoice;

trait ReceivesInvoicePayment
{
public static function getDefaultName(): ?string
{
return 'receive_payment';
}

protected function setUp(): void
{
parent::setUp();

$this->button();

$this->visible(static fn (Invoice $record) => blank($record->paid_at));

// more method...
}
}
<?php

namespace App\Filament\Actions\Concerns;

use App\Models\Invoice;

trait ReceivesInvoicePayment
{
public static function getDefaultName(): ?string
{
return 'receive_payment';
}

protected function setUp(): void
{
parent::setUp();

$this->button();

$this->visible(static fn (Invoice $record) => blank($record->paid_at));

// more method...
}
}
DrByte
DrByte12mo ago
That's the most common way I've seen people say they're doing it. Looks sensible to me! ... and that's the approach I'm using in my apps.
Want results from more Discord servers?
Add your server