I need to customize some Laravel Filament v3 Resource Form Fields/Elements by the current page type index/create/edit/view
. For example, hide some action Fields on a view page.
Resource PHP file (Bar)
<?php
namespace AppFilamentResources;
use AppFilamentResourcesBarResourcePages;
use FilamentFormsComponentsTextInput;
use FilamentFormsForm;
class BarResource extends FilamentResourcesResource
{
public static function getPages(): array
{
return [
'index' => PagesListBars::route('/'),
'create' => PagesCreateBar::route('/create'),
'edit' => PagesEditBar::route('/{record:uuid}/edit'),
'view' => PagesViewBar::route('/{record:uuid}/view'),
];
}
public static function form(Form $form): Form
{
return $form
->schema([
TextInput::make('name')
->hidden(function () {
//do something in cases of edit/view states, something like $state
/*switch ($state) {
case 'view':
return true;
break;
}*/
}),
]);
}
}
Any ideas how to implement switching (get current) resource page type in a form?
2
Answers
💡 I got an ideas that solves the issue.
Examples below are based on
edit
context. ⚠️ Keep in mind: also you have to make it withcreate/view/index
contexts. Let's create a public property$crud
that determines wich context is backed in a given resource class.1️⃣ VIA MIDDLE CLASSES
CreateRecord
classAppOverrideFilamentResourcesPagesCreateRecord
EditRecord
classAppOverrideFilamentResourcesPagesEditRecord
ViewRecord
classAppOverrideFilamentResourcesPagesViewRecord
ListRecords
classAppOverrideFilamentResourcesPagesListRecords
EditRecord child class
appOverrideFilamentResourcesPagesEditRecord.php
Bar Edit
appFilamentResourcesBarResourcePagesEditBar.php
2️⃣ VIA TRAITS
CreateRecord
traitAppTraitsFilamentResourcesPagesCreateRecord
EditRecord
traitAppTraitsFilamentResourcesPagesEditRecord
ViewRecord
traitAppTraitsFilamentResourcesPagesViewRecord
ListRecords
traitAppTraitsFilamentResourcesPagesListRecords
EditRecord trait
appTraitsFilamentResourcesPagesEditRecord.php
Bar Edit
appFilamentResourcesBarResourcePagesEditBar.php
💯 RESULT 💎
Bar Resource
appFilamentResourcesBarResource.php
Now you can switch
$form->getLivewire()->crud
to get a current form context.Great that you solve the problem, and nice to see to achieve this with flexible method.
On the other hand though, there are two ways available by Filament.
First:
or
Read more from the Docs
Second:
or
Read more from the Doc