
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(): array
16 {
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): Form
26 {
27 return $form
28 ->model($this->model)
29 ->schema([
30 ...
31 }
32
33
34 public function table(Table $table): Table
35 {
36 return $table
37 ->query($this->model::query())
38 ->columns([
39 ...
40 }
41
42 protected function getHeaderActions(): array
43 {
44 return [
45 ...parent::getHeaderActions(),
46 SelectAction::make('model')
47 ->options(static::getModels())
48 ...
49 ];
50 }
51
52 public function updatedModel(): void
53 {
54 $this->resetTable();
55 }
56
57 ...
58
59}
60
61//cms-resource.blade.php
62<x-filament-panels::page>
63 {{ $this->table }}
64</x-filament-panels::page>

Related Tricks:
How to Apply Authorization on Create Option Action for Select Field
I’ll guide you on how to test your Form Builder using Livewire Volt with a class-based component.
Bring the sticky actions back form filament v2
Let Users to Select All Options with a Simple Hint Action
make all Field or any components translatable