
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:
make all Field or any components translatable
I’ll guide you on how to test your Form Builder using Livewire Volt with a class-based component.
Let Users to Select All Options with a Simple Hint Action
Custom copy action, click the icon to copy the content
Bring the sticky actions back form filament v2