nckrtl / filament-resource-templates
This is my package filament-resource-templates
Fund package maintenance!
Nick Retel
Requires
- php: ^8.2
- filament/filament: ^3.2
- filament/forms: ^3.2
- illuminate/contracts: ^11.0
- spatie/laravel-data: ^4.6
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^2.9
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1
- orchestra/testbench: ^9.0.0
- pestphp/pest: ^2.34
- pestphp/pest-plugin-arch: ^2.7
- pestphp/pest-plugin-laravel: ^2.3
- phpstan/extension-installer: ^1.3
- phpstan/phpstan-deprecation-rules: ^1.1
- phpstan/phpstan-phpunit: ^1.3
- spatie/laravel-ray: ^1.35
README
This package allows you to add the concept of templates to your Filament Resources. By utilizing DTO's to define template sections and components we can apply more structure which is useful when we need to display the content in the frontend through auto completion. This is as well beneficial for Livewire frontends as for Inertia frontends that use typescript.
Use with cause
This package is still considered a work in progress. There are no tests yet and things are still likely to change. So use with caution as future changes might result in data loss when editing records in filament using the template feature of this package.
Installation
You can install the package via composer:
composer require nckrtl/filament-resource-templates
Usage
- Prepare your models and tables
Each resource you want to use templates for needs at least a template column that can contain a string (
$table->string('template')
) and content column that will contain the structered content in json format ($table->json('content')
). By default all data of your template will be stored in the content column, but if you need data that needs to sit in their own column that's also possible.
In the example below we'll use pages as an example and we'll store the title and slug in a separate column so we can display it more easily in the table on the resource list page.
- Create a template file. A template file is basically just a DTO:
namespace App\Filament\Resources\PageResource\Pages\Default; use App\Filament\Resources\PageResource\Pages\Partials\Sections\TextSectionData; use Filament\Forms\Components\Grid; use Filament\Forms\Components\Tabs; use Filament\Forms\Components\Tabs\Tab; use Filament\Forms\Components\TextInput; use NckRtl\FilamentResourceTemplates\Template; class DefaultTemplate extends Template { const NAME = 'Default'; public string $title; public string $slug; public static function form() { return Tabs::make('Label') ->tabs([ Tab::make('General') ->schema([ Grid::make(2)->schema([ TextInput::make('title') ->placeholder('Titel') ->label('Titel'), TextInput::make('slug') ->placeholder('slug') ->prefix(config('app.url').'/') ->label('slug'), ]), ]), ]); } }
Each template at least should have a name and a form definition. In addition you can add sections and properties. When you add class properties that are used in the form definition, then those properties will be stored in their own column. So make sure the columns are added to the table beforehand.
- Add sections The form definition in the template is the first section being displayed. You can add additional sections by defining them with a const:
const SECTIONS = [ TextSectionData::SECTION_KEY => TextSectionData::class, ];
This section is a separate DTO that may look like:
namespace App\Filament\Resources\PageResource\Pages\Partials\Sections; use Filament\Forms\Components\RichEditor; use Filament\Forms\Components\Section; use NckRtl\FilamentResourceTemplates\TemplateSection; class TextSectionData extends TemplateSection { const SECTION_KEY = 'text'; public ?string $text = ''; public static function form() { return Section::make('Content')->schema( [ RichEditor::make(static::key('text')) ]); } }
- Update the class to extend TemplateResource
use NckRtl\FilamentResourceTemplates\TemplateResource; class PageResource extends TemplateResource ...
- Define a list of templates:
public static function getTemplateClasses(): Collection { return collect([ DefaultTemplate::class, ... ]); }
- Now when you create a record you should see the template dropdown and the first template will be selected, while showing the form definition defined in the template.
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.