logo light

Demonstration of using a resource for multiple typical models

Image Description
By: A7med khalid
  • Published: 07 Jul 2024 Updated: 07 Jul 2024

how to use a resource with multiple models

Demonstration of using a resource for multiple typical 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>
Back to Tricks