### Install Laravel Filament SEO package Source: https://github.com/ralphjsmit/laravel-filament-seo/blob/main/README.md Use Composer to install the package, which automatically includes the required laravel-seo dependency. ```shell composer require ralphjsmit/laravel-filament-seo ``` -------------------------------- ### Create and Edit SEO-enabled Filament Forms Source: https://github.com/ralphjsmit/laravel-filament-seo/blob/main/README.md Examples of implementing Filament Forms in Livewire components for creating and editing Eloquent models with integrated SEO fields. These snippets demonstrate the required form schema, model binding, and state management. ```php namespace App\Http\Livewire; use App\Models\Post; use Filament\Forms\Components\Card; use Filament\Forms\Components\TextInput; use Filament\Forms\Concerns\InteractsWithForms; use Filament\Forms\Contracts\HasForms; use Illuminate\Contracts\View\View; use Illuminate\Database\Eloquent\Model; use Livewire\Component; use RalphJSmit\Filament\SEO\SEO; class CreatePost extends Component implements HasForms { use InteractsWithForms; public array $data = []; public function mount(): void { $this->form->fill(); } public function render(): View { return view('livewire.create-post'); } protected function getFormSchema(): array { return [ TextInput::make('title'), Card::make([ SEO::make() ]), ]; } protected function getFormStatePath(): ?string { return 'data'; } protected function getFormModel(): Model|string|null { return Post::class; } public function submitForm() { $post = Post::create($this->form->getState()); /** Do not forget this step. */ $this->form->model($post)->saveRelationships(); } } ``` ```php namespace App\Http\Livewire; use App\Models\Post; use Filament\Forms\Components\TextInput; use Filament\Forms\Concerns\InteractsWithForms; use Filament\Forms\Contracts\HasForms; use Illuminate\Contracts\View\View; use Illuminate\Database\Eloquent\Model; use Livewire\Component; use RalphJSmit\Filament\SEO\SEO; class EditPost extends Component implements HasForms { use InteractsWithForms; public Post $post; public function mount(): void { $this->form->fill([ 'title' => $this->post->title, ]); } public function render(): View { return view('livewire.edit-post'); } protected function getFormSchema(): array { return [ TextInput::make('title'), SEO::make(), ]; } protected function getFormModel(): Model|string|null { return $this->post; } public function submitForm() { $this->post->update( $this->form->getState(), ); } } ``` -------------------------------- ### Configure Robots Meta Tag Options Source: https://context7.com/ralphjsmit/laravel-filament-seo/llms.txt Explains how to include the robots field in SEO forms and provides examples for updating robots directives programmatically on a model. ```php use RalphJSmit\Filament\SEO\SEO; // Include robots in your SEO form SEO::make(['title', 'description', 'robots']); // Example: Setting robots programmatically on the model $post = Post::find(1); $post->seo->update([ 'title' => 'My SEO Title', 'description' => 'Meta description for search results', 'robots' => 'noindex, nofollow', ]); ``` -------------------------------- ### Basic SEO::make() Usage in Filament Resource Source: https://context7.com/ralphjsmit/laravel-filament-seo/llms.txt Demonstrates the basic usage of the `SEO::make()` component within a Filament resource's form schema. This component automatically includes fields for title, author, description, and robots, with data binding to the model's SEO relationship. ```php schema([ TextInput::make('title') ->required() ->maxLength(255), Textarea::make('content') ->required() ->rows(10), // SEO component automatically handles all SEO fields SEO::make(), ]); } } ``` -------------------------------- ### Implement Livewire Component for SEO Editing Source: https://context7.com/ralphjsmit/laravel-filament-seo/llms.txt Demonstrates how to create a Livewire component that integrates Filament forms with SEO fields. It handles model hydration and automatic relationship saving for SEO data. ```php namespace App\Http\Livewire; use App\Models\Post; use Filament\Forms\Components\Card; use Filament\Forms\Components\TextInput; use Filament\Forms\Components\Textarea; use Filament\Forms\Concerns\InteractsWithForms; use Filament\Forms\Contracts\HasForms; use Illuminate\Contracts\View\View; use Illuminate\Database\Eloquent\Model; use Livewire\Component; use RalphJSmit\Filament\SEO\SEO; class EditPost extends Component implements HasForms { use InteractsWithForms; public Post $post; public array $data = []; public function mount(): void { $this->form->fill([ 'title' => $this->post->title, 'slug' => $this->post->slug, 'content' => $this->post->content, ]); } protected function getFormSchema(): array { return [ Card::make([ TextInput::make('title')->required()->maxLength(255), TextInput::make('slug')->required(), Textarea::make('content')->required(), ]), Card::make([ SEO::make(), ])->heading('SEO Settings'), ]; } public function submitForm(): void { $formData = $this->form->getState(); $this->post->update([ 'title' => $formData['title'], 'slug' => $formData['slug'], 'content' => $formData['content'], ]); } } ``` -------------------------------- ### Create Blade View for SEO Forms Source: https://context7.com/ralphjsmit/laravel-filament-seo/llms.txt Provides a sample Blade template structure for rendering Filament forms within Livewire components for creating or editing records. ```blade
{{ $this->form }}
``` -------------------------------- ### Livewire Component for Creating Records with SEO (PHP) Source: https://context7.com/ralphjsmit/laravel-filament-seo/llms.txt This PHP code implements a standalone Livewire component for creating new 'Post' records. It includes form fields for title, slug, content, and integrates SEO settings using the RalphJSmitFilamentSEO package. A critical aspect is the `saveRelationships()` call after model creation to ensure SEO data is persisted. ```php form->fill(); } public function render(): View { return view('livewire.create-post'); } protected function getFormSchema(): array { return [ Card::make([ TextInput::make('title') ->required() ->maxLength(255), TextInput::make('slug') ->required(), Textarea::make('content') ->required(), ]), Card::make([ SEO::make(), ])->heading('SEO Settings'), ]; } protected function getFormStatePath(): ?string { return 'data'; } protected function getFormModel(): Model|string|null { return Post::class; } public function submitForm(): void { $formData = $this->form->getState(); // Create the post $post = Post::create([ 'title' => $formData['title'], 'slug' => $formData['slug'], 'content' => $formData['content'], ]); // IMPORTANT: Save the SEO relationship after creating the model $this->form->model($post)->saveRelationships(); session()->flash('success', 'Post created successfully!'); $this->redirect(route('posts.edit', $post)); } } ``` -------------------------------- ### Selective SEO Fields with SEO::make() Source: https://context7.com/ralphjsmit/laravel-filament-seo/llms.txt Shows how to specify which SEO fields to display using the `SEO::make()` method by passing an array of desired field names. Available fields include 'title', 'author', 'description', and 'robots'. ```php schema([ // Only show title and description fields SEO::make(['title', 'description']), ]); } // Or show all except robots public static function formWithoutRobots(Form $form): Form { return $form->schema([ SEO::make(['title', 'author', 'description']), ]); } ``` -------------------------------- ### Integrate SEO component into Filament Resource Source: https://github.com/ralphjsmit/laravel-filament-seo/blob/main/README.md Add the SEO::make() component to your Filament form schema to render fields for managing SEO data. This component automatically handles saving and retrieving data from the model's SEO relationship. ```php public static function form(Form $form): Form { return $form->schema([ TextInput::make('title'), SEO::make(), ]); } ``` -------------------------------- ### Filament Admin Resource with SEO Management (PHP) Source: https://context7.com/ralphjsmit/laravel-filament-seo/llms.txt This PHP code defines a Filament Admin Resource for a 'Post' model, including fields for title, slug, content, and integrates SEO management using the RalphJSmitFilamentSEO package. It sets up form components, table columns, and page routing for index, create, and edit operations. ```php schema([ Section::make('Post Content') ->schema([ TextInput::make('title') ->required() ->maxLength(255), TextInput::make('slug') ->required() ->unique(ignoreRecord: true), Textarea::make('content') ->required() ->rows(10) ->columnSpanFull(), ]), Section::make('Search Engine Optimization') ->description('Configure how this post appears in search results') ->schema([ SEO::make(), ]), ]); } public static function table(Table $table): Table { return $table->columns([ TextColumn::make('title')->searchable(), TextColumn::make('created_at')->dateTime(), ]); } public static function getPages(): array { return [ 'index' => Pages\ListPosts::route('/'), 'create' => Pages\CreatePost::route('/create'), 'edit' => Pages\EditPost::route('/{record}/edit'), ]; } } ``` -------------------------------- ### Configure Eloquent Model with HasSEO Trait Source: https://context7.com/ralphjsmit/laravel-filament-seo/llms.txt Adds the `HasSEO` trait to an Eloquent model to enable the necessary SEO relationship for the Filament component. This allows the component to automatically retrieve and persist SEO data. ```php