### Install Spatie Laravel Tags Plugin Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/README.md Install the plugin via Composer and set up Spatie's laravel-tags by publishing and running migrations. ```bash composer require filament/spatie-laravel-tags-plugin:"^5.0" -W ``` ```bash # Publish migrations from spatie/laravel-tags php artisan vendor:publish --provider="Spatie\Tags\TagsServiceProvider" --tag="tags-migrations" ``` ```bash # Run migrations php artisan migrate ``` ```bash # Prepare your model with HasTags trait ``` -------------------------------- ### Install Plugin and Regenerate Autoloader Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/TROUBLESHOOTING.md Run these commands to install the plugin and regenerate the autoloader if the 'Filament\Forms\Components\SpatieTagsInput' class is not found. ```bash composer require filament/spatie-laravel-tags-plugin:"^5.0" -W" composer dump-autoload php artisan cache:clear ``` -------------------------------- ### Install spatie/laravel-tags and Publish Migrations Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/TROUBLESHOOTING.md Execute these commands if the 'Spatie\Tags\TagsServiceProvider' is not found, ensuring the spatie/laravel-tags package is installed and its service provider is registered. ```bash composer require spatie/laravel-tags:^4.0 php artisan vendor:publish --provider="Spatie\Tags\TagsServiceProvider" ``` -------------------------------- ### Install Filament Spatie Tags Plugin Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/README.md Install the plugin using Composer. The -W flag ensures all dependencies are updated. ```bash composer require filament/spatie-laravel-tags-plugin:"^5.0" -W ``` -------------------------------- ### Filament Testing Setup for SpatieTagsInput Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/TROUBLESHOOTING.md When testing components that use `SpatieTagsInput`, ensure you set up the Filament form context properly using the testing utilities. This example demonstrates creating a form, filling its state, saving relationships, and asserting tag persistence. ```php use Filament\Tests\TestCase; class SpatieTagsInputTest extends TestCase { public function test_input_syncs_tags() { $user = User::factory()->create(); // Create form with component $form = Form::make() ->model($user) ->schema([ SpatieTagsInput::make('tags')->type('tag'), ]); // Set form state $form->fill(['tags' => ['tag1', 'tag2']]); $form->saveRelationships(); // Assert tags were saved $this->assertTrue($user->hasTag('tag1', 'tag')); $this->assertTrue($user->hasTag('tag2', 'tag')); } } ``` -------------------------------- ### Install and Use Laravel Telescope Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/TROUBLESHOOTING.md For comprehensive debugging, install Laravel Telescope. It allows you to monitor database queries, inspect requests, and identify potential issues like N+1 query patterns. ```bash composer require laravel/telescope --dev php artisan telescope:install ``` -------------------------------- ### Method Chaining Example Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/API_SUMMARY.md Demonstrates method chaining in SpatieTagsInput, showing that configuration and inherited methods return static instances. ```php SpatieTagsInput::make('tags') ->type('category') // Returns static ->required() // Returns static (inherited) ->helperText('...') // Returns static (inherited) ``` -------------------------------- ### Create an Infolist Entry with SpatieTagsEntry Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/API_SUMMARY.md Example of creating an infolist entry using SpatieTagsEntry and specifying a tag type. ```php SpatieTagsEntry::make('field_name') ->type('type_name') ``` -------------------------------- ### Dynamic Tag Type Configuration Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/INDEX.md Example of configuring tag types dynamically using a closure. ```php ->type(fn () => 'dynamic') ``` -------------------------------- ### Custom Tag Model Configuration Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/ARCHITECTURE.md Example of configuring a custom tag model in `config/tags.php` to be used by the plugin. ```php // In config/tags.php 'tag_model' => App\Models\CustomTag::class, ``` -------------------------------- ### User Tagging Example for Security Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/README.md Illustrates attaching a tag with a specific type ('role') and checking for it. This highlights the security risk if untyped inputs are used. ```php $user->attachTag('admin', type: 'role'); // Later, in an authorisation check: if ($user->hasTag('admin', 'role')) { // ... } ``` -------------------------------- ### Defensive Error Handling Examples Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/ARCHITECTURE.md Illustrates the plugin's defensive programming approach, showing how it gracefully handles missing methods, null records, and missing relationships to prevent application crashes. ```php // Silent return on missing methods if (! method_exists($record, 'tagsWithType')) { return; } // Handle null records if (! $record) { return []; } // Handle missing relationships gracefully if (! ($record && method_exists($record, 'tags'))) { return; } ``` -------------------------------- ### Displaying Tags from a Related Model Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/api-reference/SpatieTagsEntry.md This example demonstrates how to display tags from a related model by specifying the relationship path in the `make()` method. ```php // Display tags from a related model SpatieTagsEntry::make('author.tags') ->type('author-tags') ``` -------------------------------- ### Create a Table Column with SpatieTagsColumn Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/API_SUMMARY.md Example of creating a table column using SpatieTagsColumn and specifying a tag type. ```php SpatieTagsColumn::make('field_name') ->type('type_name') ``` -------------------------------- ### Default SpatieTagsEntry Configuration Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/api-reference/SpatieTagsEntry.md This snippet shows the default setup for SpatieTagsEntry, which displays all tag types with badge styling. ```php SpatieTagsEntry::make('tags') ``` -------------------------------- ### SpatieTagsEntry Component Usage Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/INDEX.md Example of how to use the SpatieTagsEntry component to display tags with a specific type in an infolist. ```php SpatieTagsEntry::make('tags')->type('category') ``` -------------------------------- ### Test Tag Loading with Type Restriction Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/USAGE_PATTERNS.md Verify that tags are loaded correctly for a specific type. This example shows how to create a user with tags of a certain type and then test the component's loading behavior. ```php $user = User::factory()->create(); $user->syncTagsWithType(['tag1', 'tag2'], 'type1'); $component = SpatieTagsInput::make('tags')->type('type1'); // Would load: ['tag1', 'tag2'] ``` -------------------------------- ### SpatieTagsInput Component Usage Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/INDEX.md Example of how to use the SpatieTagsInput component to manage tags with a specific type. ```php SpatieTagsInput::make('tags')->type('category') ``` -------------------------------- ### Configuration Values for Spatie Tags Plugin Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/API_SUMMARY.md Examples of configuration values read by the Spatie Tags plugin, including custom tag model and application locale. ```php config('tags.tag_model') // Custom tag model class (default: \Spatie\Tags\Tag::class) app()->getLocale() // Current application locale for storing tag names ``` -------------------------------- ### Get Tag Suggestions Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/api-reference/SpatieTagsInput.md Retrieves available tag suggestions for autocomplete. Can be overridden with custom suggestions. ```php public function getSuggestions(): array ``` ```php $component = SpatieTagsInput::make('tags') ->type('categories'); $suggestions = $component->getSuggestions(); // Returns: ['electronics', 'clothing', 'home'] ``` ```php $component = SpatieTagsInput::make('tags') ->suggestions(['custom', 'tags']); $suggestions = $component->getSuggestions(); // Returns: ['custom', 'tags'] ``` -------------------------------- ### AllTagTypes Marker Class Usage Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/INDEX.md Example of using the AllTagTypes marker class to indicate that tags of all types are allowed. ```php ->type(new AllTagTypes) ``` -------------------------------- ### Create a Form Field with SpatieTagsInput Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/API_SUMMARY.md Example of creating a form field using SpatieTagsInput and specifying a tag type. ```php SpatieTagsInput::make('field_name') ->type('type_name') ``` -------------------------------- ### SpatieTagsColumn Component Usage Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/INDEX.md Example of how to use the SpatieTagsColumn component to display tags with a specific type in a table. ```php SpatieTagsColumn::make('tags')->type('category') ``` -------------------------------- ### Get Tag Class Name from Configuration Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/ARCHITECTURE.md Retrieves the tag model class name from the configuration, defaulting to Spatie's Tag class. ```php $tagClassName = config('tags.tag_model', Tag::class); ``` -------------------------------- ### Null Safety: Handling Null Type Input Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/API_SUMMARY.md Examples showing that Spatie Tags plugin methods gracefully handle null inputs for the 'type' parameter, indicating untyped tags. ```php SpatieTagsInput::make('tags')->type(null) // Valid: null type (untyped tags) ``` ```php SpatieTagsColumn::make('tags')->type(null) // Valid: null type (untyped tags) ``` ```php SpatieTagsEntry::make('tags')->type(null) // Valid: null type (untyped tags) ``` -------------------------------- ### Configure SpatieTagsInput Validation Rules Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/TROUBLESHOOTING.md When form validation fails on the tags field, ensure the validation rules are correctly applied to the `SpatieTagsInput` component. This example shows how to set required, maxItems, and minItems. ```php SpatieTagsInput::make('tags') ->required() // Field is required ->maxItems(5) // Maximum 5 tags ->minItems(1) // Minimum 1 tag ``` -------------------------------- ### Project File Structure Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/MANIFEST.md This is the directory structure for the output documentation files. ```bash output/ ├── INDEX.md (Start here) ├── README.md (Quick overview) ├── API_SUMMARY.md (Quick reference) ├── USAGE_PATTERNS.md (Examples) ├── ARCHITECTURE.md (Deep dive) ├── TROUBLESHOOTING.md (Solutions) ├── MANIFEST.md (This file) └── api-reference/ ├── SpatieTagsInput.md (Form component) ├── SpatieTagsColumn.md (Table component) ├── SpatieTagsEntry.md (Infolist component) └── AllTagTypes.md (Type marker) ``` -------------------------------- ### Run Database Migrations Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/README.md Execute pending database migrations to create the necessary tables. ```bash php artisan migrate ``` -------------------------------- ### Untagged Tag Type Configuration Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/INDEX.md Example of configuring the component to only allow untagged items. ```php type(null) ``` -------------------------------- ### Publish and Run Tag Migrations Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/TROUBLESHOOTING.md Run these commands to publish and execute the necessary migrations for the spatie/laravel-tags package if the 'tags' table is not found. ```bash php artisan vendor:publish --provider="Spatie\Tags\TagsServiceProvider" --tag="tags-migrations" php artisan migrate ``` -------------------------------- ### Package Structure Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/API_SUMMARY.md Illustrates the directory structure of the Spatie Laravel Tags Plugin, showing the location of key components within the src directory. ```bash filament/spatie-laravel-tags-plugin ├── src/ │ ├── Forms/Components/ │ │ └── SpatieTagsInput.php │ ├── Infolists/Components/ │ │ └── SpatieTagsEntry.php │ ├── Tables/Columns/ │ │ └── SpatieTagsColumn.php │ └── SpatieLaravelTagsPlugin/Types/ │ └── AllTagTypes.php ├── composer.json └── README.md ``` -------------------------------- ### Create AllTagTypes Instance using make() Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/api-reference/AllTagTypes.md Instantiate AllTagTypes using the static make() method, which resolves the instance via the application container. This is useful for dependency injection. ```php $allTypes = AllTagTypes::make(); ``` -------------------------------- ### Import SpatieTagsEntry Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/api-reference/SpatieTagsEntry.md Import the SpatieTagsEntry component into your PHP file. ```php use Filament\Infolists\Components\SpatieTagsEntry; ``` -------------------------------- ### Integration Test: Syncing Tags Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/ARCHITECTURE.md Demonstrates integration testing by creating a user, syncing tags with a specific type, and asserting the state of a form component. ```php $user = User::factory()->create(); $user->syncTagsWithType(['tag1', 'tag2'], 'type1'); $form = Filament\Forms\Form::make() ->schema([ SpatieTagsInput::make('tags')->type('type1'), ]) ->model($user); $this->assertEquals(['tag1', 'tag2'], $form->getState()['tags']); ``` -------------------------------- ### Import SpatieTagsColumn Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/api-reference/SpatieTagsColumn.md Import the SpatieTagsColumn class to use it in your Filament tables. ```php use Filament\Tables\Columns\SpatieTagsColumn; ``` -------------------------------- ### Get Configured Tag Type Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/api-reference/SpatieTagsColumn.md The getType() method retrieves the tag type configuration for the column. If a Closure was provided to type(), it is evaluated here. ```php $column = SpatieTagsColumn::make('tags')->type('categories'); $type = $column->getType(); // Returns 'categories' ``` ```php $column = SpatieTagsColumn::make('tags')->type(new AllTagTypes); $type = $column->getType(); // Returns AllTagTypes instance ``` -------------------------------- ### AllTagTypes make() Method Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/API_SUMMARY.md Creates a new instance of AllTagTypes from the service container. ```php public static function make(): static ``` -------------------------------- ### Get Configured Tag Type Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/api-reference/SpatieTagsInput.md Retrieve the tag type configured for the SpatieTagsInput field. This method evaluates any Closures provided to the type() method. ```php $component = SpatieTagsInput::make('tags')->type('categories'); $type = $component->getType(); // Returns 'categories' ``` ```php $component = SpatieTagsInput::make('tags')->type(new AllTagTypes); $type = $component->getType(); // Returns AllTagTypes instance ``` -------------------------------- ### Instantiate SpatieTagsEntry Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/api-reference/SpatieTagsEntry.md Create a new SpatieTagsEntry component using the static make() method. The name parameter corresponds to the Eloquent model attribute or relationship. ```php SpatieTagsEntry::make(string $name) ``` -------------------------------- ### Basic SpatieTagsEntry Usage Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/README.md Use this snippet to display a default set of tags in an infolist entry. Ensure your model is set up to attach tags according to Spatie's documentation. ```php use Filament\Infolists\Components\SpatieTagsEntry; SpatieTagsEntry::make('tags') ``` -------------------------------- ### Configure Resource for SpatieTagsEntry Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/TROUBLESHOOTING.md Ensure the model is correctly loaded within the resource's ViewAction or ViewPage configuration to properly display data in SpatieTagsEntry. ```php // In your resource's ViewAction or ViewPage protected function getHeaderActions(): array { return [ ViewAction::make() ->form(fn (Model $record) => $this->form($record)), // Ensure model is set ]; } ``` -------------------------------- ### Instantiate SpatieTagsInput Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/api-reference/SpatieTagsInput.md Create a new SpatieTagsInput component using the static make() method. The name parameter corresponds to the Eloquent model attribute. ```php SpatieTagsInput::make(string $name) ``` -------------------------------- ### Get Tag State for Display Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/api-reference/SpatieTagsEntry.md The getState() method fetches tags from the model for display, handling both direct attributes and relationship data. Results are deduplicated to ensure unique tag names. ```php // On a User model with tags $entry = SpatieTagsEntry::make('tags')->type('categories'); // In infolist context, automatically called to fetch data // Returns: ['electronics', 'clothing'] for the given record $tags = $entry->getState(); ``` -------------------------------- ### Form, Table, and Infolist Components Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/README.md Demonstrates the basic usage of SpatieTagsInput for forms, SpatieTagsColumn for tables, and SpatieTagsEntry for infolists. The 'type' parameter can be used to filter tags by a specific category. ```php use Filament\Forms\Components\SpatieTagsInput; SpatieTagsInput::make('tags') ->type('categories') // Optional: restrict to a specific type ``` ```php use Filament\Tables\Columns\SpatieTagsColumn; SpatieTagsColumn::make('tags') ->type('categories') ``` ```php use Filament\Infolists\Components\SpatieTagsEntry; SpatieTagsEntry::make('tags') ->type('categories') ``` -------------------------------- ### Instantiate SpatieTagsColumn Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/api-reference/SpatieTagsColumn.md Create a new SpatieTagsColumn component using the static make() method. The name parameter corresponds to the Eloquent model attribute or relationship. ```php SpatieTagsColumn::make('categories') ``` -------------------------------- ### Import SpatieTagsInput Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/api-reference/SpatieTagsInput.md Import the SpatieTagsInput component into your PHP file. ```php use Filament\Forms\Components\SpatieTagsInput; ``` -------------------------------- ### Create Tag with Locale-Indexed Name Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/ARCHITECTURE.md Demonstrates how to create a new tag, ensuring its name is stored correctly for multilingual support using the current application locale. ```php $locale = $tagClassName::getLocale(); $tag = $tagClassName::create([ 'name' => [$locale => $tagName], // Store with locale key ]); ``` -------------------------------- ### SpatieTagsEntry Instantiation Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/api-reference/SpatieTagsEntry.md Creates a new SpatieTagsEntry component. This is the standard way to instantiate the component, similar to other Filament components. ```APIDOC ## SpatieTagsEntry::make() ### Description Creates a new SpatieTagsEntry component. ### Method `SpatieTagsEntry::make(string $name)` ### Parameters #### Path Parameters - **$name** (string) - Required - The field name, which corresponds to the Eloquent model attribute or relationship. ``` -------------------------------- ### Return Type: Array of Tag Name Strings Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/API_SUMMARY.md Specifies that the `getSuggestions()` and `getState()` methods return an array of tag name strings. ```php array // Array of tag name strings ``` -------------------------------- ### Import AllTagTypes Class Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/api-reference/AllTagTypes.md Import the AllTagTypes class for use in your Filament components. ```php use Filament\SpatieLaravelTagsPlugin\Types\AllTagTypes; ``` -------------------------------- ### Enable Live Updates for Component State Inspection Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/TROUBLESHOOTING.md In your resource form, enable live updates on the form using the `live()` method. This allows you to inspect the component's state in real-time, which can be helpful for debugging form behavior. ```php // In your resource form protected function getForms(): array { return [ 'form' => $this->form() ->live() // Enable live updates for debugging ->columns(1), ]; } ``` -------------------------------- ### Evaluate Dynamic Configuration Values Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/ARCHITECTURE.md This method evaluates configuration values, which can be closures, to determine dynamic settings like tag types at runtime. It's inherited from Filament's component system. ```php public function getType(): string | AllTagTypes | null { return $this->evaluate($this->type); } ``` -------------------------------- ### Configure SpatieTagsInput with AllTagTypes (Shorthand) Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/api-reference/AllTagTypes.md An alternative way to configure SpatieTagsInput with AllTagTypes using the 'new' operator. This is often seen as more concise. ```php SpatieTagsInput::make('tags') ->type(new AllTagTypes) ``` -------------------------------- ### getSuggestions() Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/api-reference/SpatieTagsInput.md Retrieves available tag suggestions for autocomplete functionality. It can fetch tags from the database based on a configured type or use custom suggestions provided via the `suggestions()` method. ```APIDOC ## getSuggestions() ### Description Retrieves available tag suggestions for autocomplete functionality. It can fetch tags from the database based on a configured type or use custom suggestions provided via the `suggestions()` method. ### Method ```php public function getSuggestions(): array ``` ### Returns `array` — An array of tag name strings available as suggestions. ### Example ```php $component = SpatieTagsInput::make('tags') ->type('categories'); $suggestions = $component->getSuggestions(); // Returns: ['electronics', 'clothing', 'home'] // With custom suggestions (overrides database query) $component = SpatieTagsInput::make('tags') ->suggestions(['custom', 'tags']); $suggestions = $component->getSuggestions(); // Returns: ['custom', 'tags'] ``` ``` -------------------------------- ### AllTagTypes::make() Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/API_SUMMARY.md Creates a new instance of AllTagTypes, typically from the service container. ```APIDOC ## AllTagTypes::make() ### Description Creates a new instance of AllTagTypes, typically from the service container. ### Method `public static function make(): static` ### Returns Instance of AllTagTypes from the service container. ``` -------------------------------- ### AllTagTypes::make() Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/INDEX.md Creates an instance of the AllTagTypes class. This is a static factory method used to instantiate the marker class for type configuration. ```APIDOC ## AllTagTypes::make() ### Description Creates an instance of the AllTagTypes class. This is a static factory method used to instantiate the marker class for type configuration. ### Method `static make()` ### Parameters None ### Response - An instance of `AllTagTypes`. ### Example ```php use Filament\Forms\Components\TextInput; use Filament\Forms\Form; use Spatie\LaravelTagsPlugin\Types\AllTagTypes; // ... Form::make() ->schema([ TextInput::make('tags') ->type(new AllTagTypes), ]) ->saveRelationships(); ``` ``` -------------------------------- ### Null Safety: Handling Null Model Input Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/API_SUMMARY.md Demonstrates that the `syncTagsWithAnyType` method safely handles null model inputs by performing a no-operation and returning early. ```php // Safely handles null record syncTagsWithAnyType(null, ['tag1', 'tag2']) // No-op, returns early ``` -------------------------------- ### Ensure Consistent Tag Model Configuration Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/TROUBLESHOOTING.md To avoid conflicts with other packages using Spatie tags, ensure all packages are configured to use the same tag model in `config/tags.php`. If using custom models, they should extend the base Tag class or implement the same interface. ```php 'tag_model' => \Spatie\Tags\Tag::class, ``` -------------------------------- ### SpatieTagsInput Load Lifecycle Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/ARCHITECTURE.md This code snippet demonstrates how the `SpatieTagsInput` component loads its state from relationships, branching logic based on whether any tag type is allowed or a specific type is configured. ```php $this->loadStateFromRelationshipsUsing(static function (SpatieTagsInput $component, ?Model $record): void { if (! method_exists($record, 'tagsWithType')) { return; } $type = $component->getType(); $record->load('tags'); if ($component->isAnyTagTypeAllowed()) { $tags = $record->getRelationValue('tags'); } else { $tags = $record->tagsWithType($type); } $component->state($tags->pluck('name')->all()); }); ``` -------------------------------- ### Finding Tags by String and Type Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/USAGE_PATTERNS.md Retrieve tags using Spatie's helper methods. Find a tag by its name across all types or specifically within a given type. ```php // For AllTagTypes (no type restriction) $tag = Tag::findFromStringOfAnyType('laravel'); // For specific type $tags = Post::find(1)->tagsWithType('technology'); ``` -------------------------------- ### Configure SpatieTagsInput with a Specific Type (Safe) Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/api-reference/AllTagTypes.md Demonstrates how to configure SpatieTagsInput with a specific tag type string to restrict access, which is a safer practice when tag types represent privileges. ```php SpatieTagsInput::make('hobbies') ->type('hobbies') ``` -------------------------------- ### SpatieTagsInput Constructor Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/api-reference/SpatieTagsInput.md Creates a new SpatieTagsInput component. This is the standard way to instantiate the component, similar to other Filament components. ```APIDOC ## SpatieTagsInput::make() ### Description Creates a new SpatieTagsInput component. Standard Filament component instantiation via the static `make()` method. ### Method Signature ```php SpatieTagsInput::make(string $name) ``` ### Parameters #### Path Parameters - **$name** (string) - Required - The field name, which corresponds to the Eloquent model attribute. ``` -------------------------------- ### Limit Tag Suggestions for Performance Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/TROUBLESHOOTING.md Improve form loading performance by limiting the number of suggestions queried for each request. Use `limit()` or cache the suggestions. ```php SpatieTagsInput::make('tags') ->suggestions(function () { return Tag::query() ->where('type', 'tag') ->limit(100) // Limit suggestions ->pluck('name'); }) ``` ```php SpatieTagsInput::make('tags') ->suggestions(function () { return cache()->remember('tag-suggestions', 3600, function () { return Tag::pluck('name')->toArray(); }); }) ``` -------------------------------- ### Creating Tags with Types Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/USAGE_PATTERNS.md Tags are automatically created with their specified type when a user enters them in a component configured with a type. This simplifies tag management. ```php // User enters "laravel" in a field with type('technology') // The component creates: Tag::create(['name' => 'laravel', 'type' => 'technology']) ``` -------------------------------- ### Configuring Tag Types in Spatie Laravel Tags Plugin Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/INDEX.md Demonstrates various ways to set the tag type, including specific string types, untyped tags, all types by default, and dynamically evaluated types. ```php // Type options ->type('string-type') // Specific type ->type(null) // Untyped only ->type(new AllTagTypes) // All types (default) ->type(fn () => 'dynamic') // Runtime evaluation ``` -------------------------------- ### SpatieTagsColumn Constructor Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/api-reference/SpatieTagsColumn.md Creates a new SpatieTagsColumn component. This is the standard way to instantiate Filament components using the static `make()` method. ```APIDOC ## SpatieTagsColumn::make() ### Description Creates a new SpatieTagsColumn component. This method is inherited from Filament's base column components. ### Method static make ### Parameters #### Path Parameters - **$name** (string) - Required - The field name, which corresponds to the Eloquent model attribute or relationship containing the tags. ``` -------------------------------- ### Empty State Handling for SpatieTagsEntry Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/api-reference/SpatieTagsEntry.md When a model has no associated tags, `getState()` returns an empty array, resulting in no badge elements being rendered for the entry. ```php $entry = SpatieTagsEntry::make('tags'); // If the model has no tags: $entry->getState() returns [] // This renders as an empty badge group ``` -------------------------------- ### Display Multiple Tag Types in Infolist Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/USAGE_PATTERNS.md Show different types of tags in separate infolist entries. Each entry can be configured with a specific type and label. ```php SpatieTagsEntry::make('categories') ->type('category') ->label('Categories'), SpatieTagsEntry::make('technologies') ->type('technology') ->label('Technologies'), SpatieTagsEntry::make('contributors') ->type('contributor') ->label('Contributors') ``` -------------------------------- ### Configure Tag Model Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/TROUBLESHOOTING.md Ensure the tag model is correctly configured in the `config/tags.php` file. This is crucial for the plugin to correctly identify and manage tags. ```php // In config/tags.php 'tag_model' => \Spatie\Tags\Tag::class, ``` -------------------------------- ### applyEagerLoading Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/api-reference/SpatieTagsColumn.md Optimizes database queries by adding eager loading for the tags relationship. This method is automatically called by Filament to prevent N+1 query problems when displaying tags for multiple records. ```APIDOC ## applyEagerLoading() ### Description Optimizes database queries by adding eager loading for tags relationship. Called by Filament to optimize queries before fetching records for the table. If the column is not hidden and has a relationship name, it eager-loads the related model's `tags` relationship. If no relationship is configured, it eager-loads the primary model's `tags` relationship directly. This prevents N+1 query problems when displaying tags for many records. ### Method `applyEagerLoading` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters #### Method Parameters - **$query** (Builder | Relation) - Required - The Eloquent query builder or relation instance. ### Request Example ```php // Applied automatically in table context $query = Post::query(); $column = SpatieTagsColumn::make('tags'); $optimizedQuery = $column->applyEagerLoading($query); // Result: query now has with(['tags']) or with(['category.tags']) applied ``` ### Response #### Success Response - **Builder | Relation** — The modified query with tags relation eager-loaded #### Response Example None provided. ``` -------------------------------- ### Build Tag Suggestions Query Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/ARCHITECTURE.md This method constructs a query for tag suggestions, adapting the WHERE clause based on whether any tag type is allowed or a specific type is configured. It handles custom suggestion overrides. ```php public function getSuggestions(): array { if ($this->suggestions !== null) { return parent::getSuggestions(); // Custom suggestions override } $type = $this->getType(); $query = $tagClass::query(); // Type-aware WHERE clause if (! $this->isAnyTagTypeAllowed()) { $query->when( filled($type), fn (Builder $query) => $query->where('type', $type), fn (Builder $query) => $query->where('type', null), ); } return $query->pluck('name')->all(); } ``` -------------------------------- ### Verify Custom Tag Model Configuration Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/TROUBLESHOOTING.md After setting a custom tag model, you can verify that the plugin is reading the configuration correctly by checking the `config('tags.tag_model')` value in Tinker. ```php php artisan tinker >>> config('tags.tag_model'); // Should show your custom model ``` -------------------------------- ### Default Behavior of Spatie Tag Components Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/api-reference/AllTagTypes.md Demonstrates that SpatieTagsInput, SpatieTagsColumn, and SpatieTagsEntry default to using AllTagTypes for their type configuration, allowing all tag types. ```php // SpatieTagsInput defaults to AllTagTypes $input = SpatieTagsInput::make('tags'); // Internally: $this->type(new AllTagTypes) // SpatieTagsColumn defaults to AllTagTypes $column = SpatieTagsColumn::make('tags'); // Internally: $this->type(AllTagTypes::make()) // SpatieTagsEntry defaults to AllTagTypes $entry = SpatieTagsEntry::make('tags'); // Internally: $this->type(AllTagTypes::make()) ``` -------------------------------- ### getState() Method Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/api-reference/SpatieTagsEntry.md Retrieves the tag state for display in the entry. This method fetches and formats tags from the associated model, respecting the configured type. ```APIDOC ## getState() ### Description Retrieves the tag state for display in the entry. ### Method `public function getState(): array` ### Returns `array` — An array of unique tag name strings to display. ### Example ```php // On a User model with tags $entry = SpatieTagsEntry::make('tags')->type('categories'); // In infolist context, automatically called to fetch data // Returns: ['electronics', 'clothing'] for the given record $tags = $entry->getState(); ``` ``` -------------------------------- ### Configure Tag Model Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/USAGE_PATTERNS.md Ensure the Spatie tag model is correctly configured in your application's `config/tags.php` file. This is a common troubleshooting step if tags are not functioning as expected. ```php // In your config 'tag_model' => \Spatie\Tags\Tag::class, ``` -------------------------------- ### Publish Spatie Tags Migration Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/README.md Publish the migration file for the tags table using the vendor:publish Artisan command. ```bash php artisan vendor:publish --provider="Spatie\Tags\TagsServiceProvider" --tag="tags-migrations" ``` -------------------------------- ### SpatieTagsInput getSuggestions() Method Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/API_SUMMARY.md Retrieves an array of tag name strings for autocomplete suggestions in SpatieTagsInput. ```php public function getSuggestions(): array ``` -------------------------------- ### Check if Component Allows All Tag Types Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/api-reference/AllTagTypes.md Illustrates how to check if a component is configured to allow all tag types using an instanceof check against the AllTagTypes class. ```php $type = $component->getType(); if ($type instanceof AllTagTypes) { // Component allows all tag types } else { // Component is restricted to a specific type or null type } ``` -------------------------------- ### Apply Eager Loading for Tags Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/api-reference/SpatieTagsColumn.md Optimizes database queries by adding eager loading for the tags relationship. This is automatically applied by Filament to prevent N+1 query problems. ```php public function applyEagerLoading(Builder | Relation $query): Builder | Relation { // ... implementation details ... } ``` ```php // Applied automatically in table context $query = Post::query(); $column = SpatieTagsColumn::make('tags'); $optimizedQuery = $column->applyEagerLoading($query); // Result: query now has with(['tags']) or with(['category.tags']) applied ``` -------------------------------- ### Return Type: Eloquent Builder or Relation Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/API_SUMMARY.md Specifies that `applyEagerLoading()` returns either an Eloquent query builder or a relation. ```php Builder | Relation // Eloquent query builder or relation ``` -------------------------------- ### Enable Verbose Logging for Filament Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/TROUBLESHOOTING.md To aid in debugging, you can enable verbose logging for Filament by configuring a dedicated channel in `config/logging.php` and setting the log level to 'debug'. ```php // config/logging.php 'channels' => [ 'filament' => [ 'driver' => 'single', 'path' => storage_path('logs/filament.log'), 'level' => env('LOG_LEVEL', 'debug'), ], ], ``` -------------------------------- ### Configure SpatieTagsInput with AllTagTypes Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/api-reference/AllTagTypes.md Use AllTagTypes to configure a SpatieTagsInput component, allowing it to handle tags of any type. This is the common usage pattern. ```php use Filament\Forms\Components\SpatieTagsInput; use Filament\SpatieLaravelTagsPlugin\Types\AllTagTypes; SpatieTagsInput::make('tags') ->type(AllTagTypes::make()) ``` -------------------------------- ### Check for Existing Tags in Tinker Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/TROUBLESHOOTING.md Use this Artisan Tinker command to verify if any tags exist in the database for the SpatieTagsInput component. ```php php artisan tinker >>> \Spatie\Tags\Tag::all(); ``` -------------------------------- ### SpatieTagsColumn applyEagerLoading() Method Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/API_SUMMARY.md Applies eager loading for the tags relation to an Eloquent query builder or relation. ```php public function applyEagerLoading(Builder | Relation $query): Builder | Relation ``` -------------------------------- ### Basic SpatieTagsColumn Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/README.md Display tags in a Filament table using the SpatieTagsColumn. Ensure your Eloquent model is prepared for attaching tags. ```php use Filament\Tables\Columns\SpatieTagsColumn; SpatieTagsColumn::make('tags') ``` -------------------------------- ### SpatieTagsColumn getState() Method Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/API_SUMMARY.md Retrieves the array of tag name strings to be displayed in the SpatieTagsColumn. ```php public function getState(): array ``` -------------------------------- ### Configure Custom Tag Model Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/TROUBLESHOOTING.md To use a custom tag model, verify that the `tag_model` configuration in `config/tags.php` points to your custom model class. After changing the configuration, clear the config cache. ```php // config/tags.php 'tag_model' => App\Models\CustomTag::class, ``` -------------------------------- ### SpatieTagsEntry Component API Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/MANIFEST.md Documentation for the SpatieTagsEntry infolist entry component, including methods for setting tag types and retrieving tag data. ```APIDOC ## SpatieTagsEntry ### Description Represents an infolist entry component for displaying tags using spatie/laravel-tags. ### Methods #### `type(?string | (string|null) $type The type of tag to filter by. Defaults to null (all types allowed). Returns the instance ofA closure that returns the tag type. Defaults to null (all types allowed). #### `getType(): string| (string|null) $type The type of tag to filter by. Defaults to null (all types allowed). Returns the instance of the component. #### `getType(): ?string` Get the configured tag type filter. #### `isAnyTagTypeAllowed(): bool` Check if all tag types are allowed. #### `getState()` Get the tags associated with the entry. ### Example ```php use Filament\Infolists\Components\SpatieTagsEntry; SpatieTagsEntry::make('tags') ->type('post') // Filter tags of type 'post' ->getState(); ``` ``` -------------------------------- ### SpatieTagsInput::getSuggestions() Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/API_SUMMARY.md Retrieves an array of tag name suggestions for autocomplete functionality in the SpatieTagsInput component. ```APIDOC ## SpatieTagsInput::getSuggestions() ### Description Retrieves an array of tag name suggestions for autocomplete functionality in the SpatieTagsInput component. ### Method `public function getSuggestions(): array` ### Returns Array of tag name strings for autocomplete. ``` -------------------------------- ### Configure Tag Type for Display Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/api-reference/SpatieTagsEntry.md Use the type() method to filter and display tags by a specific type, all types, or dynamically. Pass null to show only untyped tags. ```php // Display tags of a specific type SpatieTagsEntry::make('categories') ->type('category') ``` ```php // Display tags of any type (default) SpatieTagsEntry::make('tags') ->type(new AllTagTypes) ``` ```php // Dynamically determine which type to display SpatieTagsEntry::make('tags') ->type(fn () => auth()->user()->isAdmin() ? 'admin-tags' : 'user-tags') ``` ```php // Display only untyped tags SpatieTagsEntry::make('tags') ->type(null) ``` -------------------------------- ### Eager Load Tags for Filament Column Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/ARCHITECTURE.md The `applyEagerLoading` method ensures that tags are eagerly loaded to prevent N+1 query issues when rendering tables. It checks for existing relationships and loads tags accordingly. ```php public function applyEagerLoading(Builder | Relation $query): Builder | Relation { if ($this->isHidden()) { return $query; } if ($this->hasRelationship($query->getModel())) { // Column has a relationship → eager load its tags return $query->with(["{$this->getRelationshipName($query->getModel())}.tags"]); } // No relationship → eager load primary model's tags return $query->with(['tags']); } ``` -------------------------------- ### Sync Tags Across Any Type Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/ARCHITECTURE.md This method handles the synchronization of tags when no specific type is enforced, preventing unnecessary recreation of existing null-type tags by using `findFromStringOfAnyType`. ```php protected function syncTagsWithAnyType(?Model $record, array $state): void { $tagClassName = config('tags.tag_model', Tag::class); $tags = collect($state)->map(function (string $tagName) use ($tagClassName) { $locale = $tagClassName::getLocale(); // Find existing tag by name across all types $tag = $tagClassName::findFromStringOfAnyType($tagName, $locale); // Create if doesn't exist if ($tag?->isEmpty() ?? true) { $tag = $tagClassName::create([ 'name' => [$locale => $tagName], ]); } return $tag; })->flatten(); // Sync IDs (many-to-many) $record->tags()->sync($tags->pluck('id')); } ``` -------------------------------- ### Register SpatieTagsColumn for Eager Loading Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/TROUBLESHOOTING.md Ensure the `SpatieTagsColumn` is declared in your table resource to automatically enable eager loading and prevent N+1 query issues. ```php // In your Table (Filament table resource) protected function getColumns(): array { return [ SpatieTagsColumn::make('tags'), // Automatically eager-loads // ... other columns ]; } ``` -------------------------------- ### Customize Tag Badge Colors in Infolist Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/USAGE_PATTERNS.md Style tag badges dynamically based on their state using a callback function for the 'color' method. This allows for visual differentiation of tag statuses. ```php SpatieTagsEntry::make('status') ->type('status') ->color(fn (string $state): string => match ($state) { 'active' => 'success', 'inactive' => 'gray', default => 'info', }) ``` -------------------------------- ### Clear Caches for Configuration Changes Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/TROUBLESHOOTING.md If your configuration changes are not being applied, it's likely due to a stale config cache. Clear the configuration and general cache using Artisan commands. ```bash php artisan config:cache php artisan config:clear php artisan cache:clear ``` -------------------------------- ### Explicitly Specifying All Tag Types Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/README.md Shows how to explicitly use the AllTagTypes class for a SpatieTagsInput component, which is the default behavior and allows all tag types. ```php use Filament\SpatieLaravelTagsPlugin\Types\AllTagTypes; // Explicitly specify all types (same as default) SpatieTagsInput::make('tags')->type(new AllTagTypes) ``` -------------------------------- ### SpatieTagsColumn::applyEagerLoading() Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/API_SUMMARY.md Applies eager loading for the tags relation to the provided Eloquent query builder or relation. ```APIDOC ## SpatieTagsColumn::applyEagerLoading() ### Description Applies eager loading for the tags relation to the provided Eloquent query builder or relation. ### Method `public function applyEagerLoading(Builder | Relation $query): Builder | Relation` ### Parameters #### Parameters - `$query` (Builder | Relation) - Eloquent query builder or relation ### Returns The same query with tags relation eager-loaded. ``` -------------------------------- ### Handle Tag Display with Multiple Locales Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/TROUBLESHOOTING.md Tags are stored with locale-keyed names. To support multiple locales, use `setTranslation` to sync tag names across different languages. ```php // Tags were created with locale='en' Tag::create(['name' => ['en' => 'laravel']]); // Now changing locale to 'de' doesn't change the stored key app()->setLocale('de'); $tag->name; // Still returns 'laravel' (the 'en' value) ``` ```php $tag->setTranslation('name', 'de', 'laravel'); ``` -------------------------------- ### Eager Loading Tags in Eloquent Collections Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/USAGE_PATTERNS.md Optimize database queries when working with collections of models that have tags. Use `with('tags')` to eager load tags and prevent N+1 query problems. ```php $posts = Post::with('tags')->get(); // Each post now has tags pre-loaded // SpatieTagsColumn will not trigger additional queries ``` -------------------------------- ### Test Tag Saving Source: https://github.com/filamentphp/spatie-laravel-tags-plugin/blob/5.x/_autodocs/USAGE_PATTERNS.md Simulate the form state and test the tag saving mechanism. The `saveRelationshipsUsing` callback allows you to define how the component should sync new tags. ```php $user = User::factory()->create(); // Simulate form state $component->saveRelationshipsUsing(function ($component, $record, $state) { // $state = ['newtag1', 'newtag2'] // Component will sync these tags }); ```