Demonstration of using a resource for multiple typical models
- Published: 07 Jul 2024 Updated: 07 Jul 2024
how to use a resource with multiple models
1class CmsResource extends Page implements HasTable, HasForms 2{ 3 use InteractsWithTable; 4 use InteractsWithForms; 5 6 7 protected static string $view = 'filament.editors.pages.cms-resource'; 8 9 protected static bool $shouldRegisterNavigation = true;10 11 public string $model = Article::class;12 13 private static array $models;14 15 public static function getModels(): array16 {17 static::$models = [18 Article::class => 'article',19 News::class => 'news',20 ];21 22 return static::$models;23 }24 25 public function form(Form $form): Form26 {27 return $form28 ->model($this->model)29 ->schema([30 ...31 }32 33 34 public function table(Table $table): Table35 {36 return $table37 ->query($this->model::query())38 ->columns([39 ...40 }41 42 protected function getHeaderActions(): array43 {44 return [45 ...parent::getHeaderActions(),46 SelectAction::make('model')47 ->options(static::getModels())48 ...49 ];50 }51 52 public function updatedModel(): void53 {54 $this->resetTable();55 }56 57 ...58 59}60 61//cms-resource.blade.php62<x-filament-panels::page>63 {{ $this->table }}64</x-filament-panels::page>
Related Tricks:
Sometimes, when we need to test our application locally, we always have to fill forms manually. Let's take the Filament approach and achieve this with more accurate data.
make all Field or any components translatable
Translating components can often be a repetitive task, Fortunately, there's a neat trick to automate this process, making your components instantly translatable.
I’ll guide you on how to test your Form Builder using Livewire Volt with a class-based component.
how to use a resource with multiple models