F
Filament15mo ago
Finn

Section of fields that I can re-use over multiple resources/custom pages

I want to make a custom field like SEOData::make() which contains a few fields that go to a relationship of that model. But I dont know if a custom field is the way to go, because I dont want to make a custom blade etc. It basically needs to be a reusable set of fields. Because multiple resources have a relationship to a model called PageMeta.php, and I want to implement those fields out of that database table into multiple resources/custom pages. I can just copy and paste those fields over and over again, but then if I need to change it, I need to do it on multiple places which of course isn't what you want. What's the best way to do this?
3 Replies
Panda
Panda15mo ago
You can define them on a resource or on a separate class as a static method. I've used a similar method in a resource class where I created a custom delete action which I needed in two places. I added the following code in my CoinsPackResource class as I was re-using this only in the same resource but you can create a separate class somewhere else and define your custom methods on it.
/**
* Custom delete action for applying pre-deletion checks.
*
* @param \Filament\Actions\DeleteAction $action
* @param \App\Models\Billing\CoinsPack $coinsPack
* @return \Filament\Actions\DeleteAction
*/
public static function customDeleteAction(): DeleteAction
{
return DeleteAction::make()
->before(function (DeleteAction $action, CoinsPack $coinsPack) {
// Make sure there are no pending transactions for this resource
// or else an exception will be thrown
if ($coinsPack->transactions()->pending()->exists()) {
Notification::make()
->warning()
->title('Cannot delete this record')
->body('Please wait till pending transactions are processed')
->danger()
->persistent()
->send();

$action->cancel();
}
});
}
/**
* Custom delete action for applying pre-deletion checks.
*
* @param \Filament\Actions\DeleteAction $action
* @param \App\Models\Billing\CoinsPack $coinsPack
* @return \Filament\Actions\DeleteAction
*/
public static function customDeleteAction(): DeleteAction
{
return DeleteAction::make()
->before(function (DeleteAction $action, CoinsPack $coinsPack) {
// Make sure there are no pending transactions for this resource
// or else an exception will be thrown
if ($coinsPack->transactions()->pending()->exists()) {
Notification::make()
->warning()
->title('Cannot delete this record')
->body('Please wait till pending transactions are processed')
->danger()
->persistent()
->send();

$action->cancel();
}
});
}
PS: I'm not familiar with Livewire and component stuff but if there's a better approach like a custom component then you should go with that.
awcodes
awcodes15mo ago
You don’t have to have custom views. You can just return an existing setup of form components. https://gist.github.com/awcodes/9784de531aea8e3bdba772a55958a999
Gist
Meta component
Meta component. GitHub Gist: instantly share code, notes, and snippets.
Finn
FinnOP15mo ago
Exactly what I am looking for! Thanks to both of you for answering! 🚀
Want results from more Discord servers?
Add your server