Laravel Filament SEO

repository·main·Indexed 41 days ago

https://github.com/ralphjsmit/laravel-filament-seo

A helper package that integrates the laravel-seo package with Filament Admin and Filament Forms. It provides a ready-to-use UI component via SEO::make() for managing SEO metadata, including title, author, description, and robots fields, for Eloquent models using the HasSEO trait.

Tokens
1K
Snippets
6
Records
6
Agent score
50%

What's inside Laravel Filament SEO

  1. Install laravel-filament-seo

    main

    Install the package via Composer. This will automatically install ralphjsmit/laravel-seo as a dependency if it is not already present in your project.

    Note: You must configure the SEO values in the seo.php config file and follow the configuration steps for the main laravel-seo package.

    composer require ralphjsmit/laravel-filament-seo
  2. Use the SEO component with standalone Filament Forms

    main

    When using standalone Filament Forms (e.g., in a Livewire component), you can include SEO::make() in your schema.

    Important: When creating a new record, you must manually call saveRelationships() on the form instance to persist the SEO data to the related model.

    // In a Livewire component using InteractsWithForms
    protected function getFormSchema(): array
    {
        return [
            TextInput::make('title'),
            Card::make([
                SEO::make()
            ]),
        ];
    }
    
    public function submitForm()
    {
        $post = Post::create($this->form->getState());
    
        /** Do not forget this step to save SEO data */
        $this->form->model($post)->saveRelationships();
    }
  3. Use the SEO component in Filament Admin

    main

    You can add SEO management to your Filament Resources by calling SEO::make() within the form() method. It works for both creating and editing records and automatically handles the title, author, and description fields.

    use App\Models\Post;
    use Filament\Resources\Form;
    use Filament\Resources\Resource;
    use Filament\Forms\Components\TextInput;
    use RalphJSmit\Filament\SEO\SEO;
    
    class PostResource extends Resource
    {
        protected static ?string $model = Post::class;
    
        public static function form(Form $form): Form
        {
            return $form->schema([
                TextInput::make('title'),
                SEO::make(),
            ]);
        }
    }
  4. Use SEO::make() in Filament forms

    main

    The SEO::make() method provides a pre-configured Group of Filament form components designed to manage SEO metadata. It automatically handles the state path (using seo) and manages the relationship with your model's SEO data.

    By default, it includes title, author, description, and robots. You can restrict which fields are displayed by passing an array of keys to the $only parameter.

    use RalphJSmit\
    Filament\SEO\SEO;
    
    // In your Filament Resource or Form schema:
    SEO::make()
  5. Filter SEO fields using the $only parameter

    main

    If you do not want to display all default SEO fields, you can pass an array of specific keys to SEO::make(). The available keys are:

    • title (TextInput)
    • author (TextInput)
    • description (Textarea)
    • robots (Select)

    This is useful when you want to limit the SEO management interface to only specific metadata types.

    // Only show title and description
    SEO::make(['title', 'description'])