
Demonstration of using a resource for multiple typical models
By: A7med khalid
- 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:
Demonstration of using a resource for multiple typical models
how to use a resource with multiple models
Trigger hint action on hover
toggle the visibility of an action when hover the component
Globally Translate all Components
make all Field or any components translatable
Bypass required when save as draft
Unrequire fields recursively when saving as draft
Disabling the cancel button on the filament editing form
Usability: Knowing whether or not the record was saved.