### Run Database Migrations Source: https://github.com/spatie/laravel-tags/blob/main/docs/installation-and-setup.md Executes all published database migrations, including those for the spatie/laravel-tags package. This creates the 'tags' and 'taggables' tables in your database. ```bash php artisan migrate ``` -------------------------------- ### Install spatie/laravel-tags via Composer Source: https://github.com/spatie/laravel-tags/blob/main/docs/installation-and-setup.md Installs the spatie/laravel-tags package using Composer. This is the primary method for adding the package to your Laravel project. ```bash composer require spatie/laravel-tags ``` -------------------------------- ### Publish Laravel Tags Migrations Source: https://github.com/spatie/laravel-tags/blob/main/docs/installation-and-setup.md Publishes the necessary database migration files for the spatie/laravel-tags package. This command makes the migration files available in your project's database/migrations directory. ```bash php artisan vendor:publish --provider="Spatie\Tags\TagsServiceProvider" --tag="tags-migrations" ``` -------------------------------- ### Publish Laravel Tags Configuration Source: https://github.com/spatie/laravel-tags/blob/main/docs/installation-and-setup.md Publishes the configuration file for the spatie/laravel-tags package. This allows customization of settings like the slugger and model class names. ```bash php artisan vendor:publish --provider="Spatie\Tags\TagsServiceProvider" --tag="tags-config" ``` -------------------------------- ### spatie/laravel-tags Configuration File Source: https://github.com/spatie/laravel-tags/blob/main/docs/installation-and-setup.md The default configuration file for the spatie/laravel-tags package. It allows customization of the slug generation, tag model, and taggable relation table names. ```php return [ /* * The given function generates a URL friendly "slug" from the tag name property before saving it. * Defaults to Str::slug (https://laravel.com/docs/master/helpers#method-str-slug) */ 'slugger' => null, /* * The fully qualified class name of the tag model. */ 'tag_model' => Spatie\Tags\Tag::class, /* * The name of the table associated with the taggable morph relation. */ 'taggable' => [ 'table_name' => 'taggables', 'morph_name' => 'taggable', /* * The fully qualified class name of the pivot model. */ 'class_name' => Illuminate\Database\Eloquent\Relations\MorphPivot::class, ] ]; ``` -------------------------------- ### Install spatie/laravel-tags via Composer Source: https://github.com/spatie/laravel-tags/blob/main/README.md Installs the spatie/laravel-tags package using Composer. This is the primary method for adding the package to your Laravel project. Ensure you have Composer installed and configured for your project. ```bash composer require spatie/laravel-tags ``` -------------------------------- ### Retrieving Models by Tags Source: https://github.com/spatie/laravel-tags/blob/main/docs/introduction.md Provides examples for querying models based on their associated tags. It covers fetching models that have any of the specified tags or all of the specified tags. ```PHP // retrieving models that have any of the given tags NewsItem::withAnyTags(['tag1', 'tag2']); // retrieve models that have all of the given tags NewsItem::withAllTags(['tag1', 'tag2']); ``` -------------------------------- ### Get All Tags Sorted Source: https://github.com/spatie/laravel-tags/blob/main/docs/advanced-usage/sorting-tags.md Retrieves all tags from the database, ordered by their `order_column` using the `ordered()` scope provided by the underlying eloquent-sortable package. ```php Tag::ordered()->get(); ``` -------------------------------- ### Get Model Tags by Type Source: https://github.com/spatie/laravel-tags/blob/main/docs/advanced-usage/using-types.md Shows how to retrieve tags associated with a specific model instance, filtered by a given type. This allows access to a model's tags within a particular collection. ```php $newsItem->tagsWithType('firstType'); // returns a collection ``` -------------------------------- ### Model Creation with Tags Source: https://github.com/spatie/laravel-tags/blob/main/docs/introduction.md Demonstrates how to create a model instance and assign tags directly during the creation process. Tags will be automatically created if they do not already exist. ```PHP NewsItem::create([ 'name' => 'testModel', 'tags' => ['tag', 'tag2'], //tags will be created if they don't exist ]); ``` -------------------------------- ### Run Laravel Tags Migrations Source: https://github.com/spatie/laravel-tags/blob/main/README.md Executes the published database migrations for the spatie/laravel-tags package. This command creates the necessary `tags` and `taggables` tables in your database. Ensure your database connection is properly configured in `.env`. ```bash php artisan migrate ``` -------------------------------- ### Using Tag Types Source: https://github.com/spatie/laravel-tags/blob/main/docs/introduction.md Shows how to associate tags with specific types, enabling categorization and differentiation of tags within the application. ```PHP $tag = Tag::findOrCreate('tag 1', 'my type'); ``` -------------------------------- ### Publish Laravel Tags Migrations Source: https://github.com/spatie/laravel-tags/blob/main/README.md Publishes the migration files for the spatie/laravel-tags package. This command allows you to customize the migration if needed before running it. It requires the package's service provider to be registered. ```bash php artisan vendor:publish --provider="Spatie\Tags\TagsServiceProvider" --tag="tags-migrations" ``` -------------------------------- ### Create Model with Tags and Attach/Detach Tags Source: https://github.com/spatie/laravel-tags/blob/main/README.md Demonstrates creating a model with initial tags and performing operations like attaching and detaching tags, including support for specific tag types. ```PHP // create a model with some tags $newsItem = NewsItem::create([ 'name' => 'The Article Title', 'tags' => ['first tag', 'second tag'], //tags will be created if they don't exist ]); // attaching tags $newsItem->attachTag('third tag'); $newsItem->attachTag('third tag','some_type'); $newsItem->attachTags(['fourth tag', 'fifth tag']); $newsItem->attachTags(['fourth_tag','fifth_tag'],'some_type'); // detaching tags $newsItem->detachTag('third tag'); $newsItem->detachTag('third tag','some_type'); $newsItem->detachTags(['fourth tag', 'fifth tag']); $newsItem->detachTags(['fourth tag', 'fifth tag'],'some_type'); ``` -------------------------------- ### Publish Laravel Tags Configuration Source: https://github.com/spatie/laravel-tags/blob/main/README.md Publishes the configuration file for the spatie/laravel-tags package. This allows you to customize package behavior, such as the slug generation method. The configuration file will be located in the `config` directory. ```bash php artisan vendor:publish --provider="Spatie\Tags\TagsServiceProvider" --tag="tags-config" ``` -------------------------------- ### Tag Translation Management Source: https://github.com/spatie/laravel-tags/blob/main/docs/introduction.md Demonstrates how to create tags and set translations for them in different languages. This feature allows for localized tag names. ```PHP $tag = Tag::findOrCreate('my tag'); $tag->setTranslation('fr', 'mon tag'); $tag->setTranslation('nl', 'mijn tag'); $tag->save(); ``` -------------------------------- ### Tag Slugs and Ordering Source: https://github.com/spatie/laravel-tags/blob/main/docs/introduction.md Explains how tags automatically generate slugs based on their names and how they are managed with an `order_column` for sorting purposes. ```PHP $tag = Tag::findOrCreate('yet another tag'); $tag->slug; //returns "yet-another-tag" // tags are sortable $tag = Tag::findOrCreate('my tag'); $tag->order_column; //returns 1 $tag2 = Tag::findOrCreate('another tag'); $tag2->order_column; //returns 2 ``` -------------------------------- ### Sync Tags and Retrieve Model Tags Source: https://github.com/spatie/laravel-tags/blob/main/README.md Shows how to synchronize a model's tags to a specific set and how to retrieve all tags associated with a model. ```PHP // syncing tags $newsItem->syncTags(['first tag', 'second tag']); // all other tags on this model will be detached // syncing tags with a type $newsItem->syncTagsWithType(['category 1', 'category 2'], 'categories'); $newsItem->syncTagsWithType(['topic 1', 'topic 2'], 'topics'); // get all tags of a model $newsItem->tags; ``` -------------------------------- ### Laravel Tags Configuration File Source: https://github.com/spatie/laravel-tags/blob/main/README.md The default configuration file for the spatie/laravel-tags package. It includes a `slugger` option to define how tag names are converted into slugs. By default, it uses Laravel's `Str::slug` helper. ```php return [ /* * The given function generates a URL friendly "slug" from the tag name property before saving it. * Defaults to Str::slug (https://laravel.com/docs/5.8/helpers#method-str-slug) */ 'slugger' => null, ]; ``` -------------------------------- ### Attaching and Detaching Tags Source: https://github.com/spatie/laravel-tags/blob/main/docs/introduction.md Shows how to add single or multiple tags to an existing model instance using `attachTag` and `attachTags`, and how to remove them using `detachTag` and `detachTags`. ```PHP // attaching tags $newsItem->attachTag('tag3'); $newsItem->attachTags(['tag4', 'tag5']); // detaching tags $newsItem->detachTag('tag3'); $newsItem->detachTags(['tag4', 'tag5']); ``` -------------------------------- ### Create Tag with Type Source: https://github.com/spatie/laravel-tags/blob/main/docs/advanced-usage/using-types.md Demonstrates how to create a new tag or find an existing one, associating it with a specific type. This allows for distinct tag collections within your application. ```php $tagWithType = Tag::findOrCreate('headline', 'newsTag'); ``` -------------------------------- ### Syncing Tags Source: https://github.com/spatie/laravel-tags/blob/main/docs/introduction.md Illustrates how to synchronize the tags associated with a model. This operation will detach any existing tags not present in the provided array and attach the new ones. ```PHP $newsItem->syncTags(['tag1', 'tag2']); // all other tags on this model will be detached ``` -------------------------------- ### Add and Retrieve Tag Translations (PHP) Source: https://github.com/spatie/laravel-tags/blob/main/docs/advanced-usage/adding-translations.md Demonstrates how to create a tag, set translations for different locales using `setTranslation`, save the model, and retrieve translations using `getTranslation` or direct property access. ```php $tag = Tag::findOrCreate('my tag'); //store in the current locale of your app //let's add some translation for other languages $tag->setTranslation('name', 'fr', 'mon tag'); $tag->setTranslation('name', 'nl', 'mijn tag'); //don't forget to save the model $tag->save(); $tag->getTranslation('name', 'fr'); // returns 'mon tag' $tag->name // returns the name of the tag in current locale of your app. ``` -------------------------------- ### Retrieve Models by Tags Source: https://github.com/spatie/laravel-tags/blob/main/README.md Query models based on their associated tags, including finding models with any, all, or none of the specified tags. ```PHP // retrieving models that have any of the given tags NewsItem::withAnyTags(['first tag', 'second tag'])->get(); // retrieve models that have all of the given tags NewsItem::withAllTags(['first tag', 'second tag'])->get(); // retrieve models that don't have any of the given tags NewsItem::withoutTags(['first tag', 'second tag'])->get(); ``` -------------------------------- ### Sync Tags by Type Source: https://github.com/spatie/laravel-tags/blob/main/docs/advanced-usage/using-types.md Explains the `syncTagsWithType` method, which allows for syncing multiple tags for a specific type onto a model. This is useful for managing tag sets for different categories. ```php $newsItem->syncTagsWithType(['tagA', 'tagB'], 'firstType'); $newsItem->syncTagsWithType(['tagC', 'tagD'], 'secondType'); ``` -------------------------------- ### Retrieve Tags by Type Source: https://github.com/spatie/laravel-tags/blob/main/docs/advanced-usage/using-types.md Demonstrates how to fetch all tags belonging to a particular type using `getWithType` or the scoped `withType` method. This is useful for listing or managing tags within a specific category. ```php $tagA = Tag::findOrCreate('tagA', 'firstType'); $tagB = Tag::findOrCreate('tagB', 'firstType'); $tagC = Tag::findOrCreate('tagC', 'secondType'); $tagD = Tag::findOrCreate('tagD', 'secondType'); Tag::getWithType('firstType'); // returns a collection with $tagA and $tagB //there's also a scoped version Tag::withType('firstType')->get(); // returns the same result ``` -------------------------------- ### Query Tags with Any/All Tags by Type Source: https://github.com/spatie/laravel-tags/blob/main/docs/advanced-usage/using-types.md Illustrates how to filter models using tag instances, including those associated with specific types. This enables querying models that have any or all of the specified tags. ```php $tag = Tag::create(['name' => 'gossip']); $tag2 = Tag::create(['name' => 'headline']); NewsItem::withAnyTags([$tag, $tag2])->get(); ``` -------------------------------- ### Tag Types and Slugs Source: https://github.com/spatie/laravel-tags/blob/main/README.md Utilize tag types to categorize tags and access the automatically generated slug for each tag. ```PHP // using tag types $tag = Tag::findOrCreate('tag 1', 'my type'); // tags have slugs $tag = Tag::findOrCreate('yet another tag'); $tag->slug; //returns "yet-another-tag" ``` -------------------------------- ### Finding Tags by Content Source: https://github.com/spatie/laravel-tags/blob/main/docs/introduction.md Demonstrates how to retrieve all tags that contain a specific string value in their name or slug. ```PHP Tag::containing('test'); // returns all tags that contain 'test' ``` -------------------------------- ### Managing Tags via Tag Model Source: https://github.com/spatie/laravel-tags/blob/main/docs/basic-usage/using-tags.md Interact with tags directly using the `SpatieTagsTag` model. This includes creating, updating, finding, and deleting tags. ```php use Spatie\Tags\Tag; // Create a tag $tag = Tag::create(['name' => 'my tag']); // Update a tag $tag->name = 'another tag'; $tag->save(); // Find a tag by its string name $tag = Tag::findFromString('another tag'); // Create a tag if it doesn't exist, or find it if it does $tag = Tag::findOrCreateFromString('yet another tag'); // Delete a tag $tag->delete(); // Find tags by string, supporting different types $tags = Tag::findFromStringOfAnyType('one more tag'); ``` -------------------------------- ### Attaching Tags to Eloquent Models Source: https://github.com/spatie/laravel-tags/blob/main/docs/basic-usage/using-tags.md Attach single or multiple tags to an Eloquent model. The package ensures tags are unique and a model has a tag attached only once. ```php // Adding a single tag $yourModel->attachTag('tag 1'); // Adding multiple tags $yourModel->attachTags(['tag 2', 'tag 3']); ``` -------------------------------- ### Manipulating Tag Order Source: https://github.com/spatie/laravel-tags/blob/main/docs/introduction.md Details how to programmatically change the order of tags, typically used for custom sorting or reordering within the application. ```PHP $tag->swapOrder($anotherTag); ``` -------------------------------- ### Syncing Tags for Eloquent Models Source: https://github.com/spatie/laravel-tags/blob/main/docs/basic-usage/using-tags.md Synchronize tags for an Eloquent model. This method ensures only the provided tags are attached, detaching any existing tags not in the new list. ```php $yourModel->syncTags(['tag 2', 'tag 3']); ``` -------------------------------- ### Tag Translation Management Source: https://github.com/spatie/laravel-tags/blob/main/README.md Manage translations for tag names and slugs. This includes setting translations for different locales and retrieving translated names. ```PHP // translating a tag $tag = Tag::findOrCreate('my tag'); $tag->setTranslation('name', 'fr', 'mon tag'); $tag->setTranslation('name', 'nl', 'mijn tag'); $tag->save(); // getting translations $tag->translate('name'); //returns my name $tag->translate('name', 'fr'); //returns mon tag (optional locale param) // convenient translations through taggable models $newsItem->tagsTranslated();// returns tags with slug_translated and name_translated properties $newsItem->tagsTranslated('fr');// returns tags with slug_translated and name_translated properties set for specified locale ``` -------------------------------- ### Tag Sorting and Order Manipulation Source: https://github.com/spatie/laravel-tags/blob/main/README.md Manage the order of tags. Tags are sortable by default, and their order can be manipulated programmatically. ```PHP // tags are sortable $tag = Tag::findOrCreate('my tag'); $tag->order_column; //returns 1 $tag2 = Tag::findOrCreate('another tag'); $tag2->order_column; //returns 2 // manipulating the order of tags $tag->swapOrder($anotherTag); ``` -------------------------------- ### Apply HasTags Trait to Eloquent Model Source: https://github.com/spatie/laravel-tags/blob/main/README.md Make an Eloquent model taggable by using the HasTags trait. This enables all tagging functionalities for the model. ```PHP use Illuminate\Database\Eloquent\Model; use Spatie\Tags\HasTags; class NewsItem extends Model { use HasTags; // ... } ``` -------------------------------- ### Retrieving All Registered Tag Types Source: https://github.com/spatie/laravel-tags/blob/main/docs/basic-usage/using-tags.md Fetch a collection of all registered tag types within the application. This can be useful for administrative interfaces or tag management. ```php use Spatie\Tags\Tag; Tag::getTypes(); ``` -------------------------------- ### Update Tag Configuration for Custom Model Source: https://github.com/spatie/laravel-tags/blob/main/docs/advanced-usage/using-another-default-locale.md Modifies the `config/tags.php` file to point to the custom `App\Models\YourTag` class. This ensures the application uses the custom model, which includes the overridden `getLocale()` method, for all tag operations. ```php return [ /* * The given function generates a URL friendly "slug" from the tag name property before saving it. * Defaults to Str::slug (https://laravel.com/docs/master/helpers#method-str-slug) */ 'slugger' => null, /* * The fully qualified class name of the tag model. */ 'tag_model' => App\Models\YourTag::class, ]; ``` -------------------------------- ### Move Tag to Start/End Source: https://github.com/spatie/laravel-tags/blob/main/docs/advanced-usage/sorting-tags.md Moves a tag instance to the very first or last position in the order using the `moveToStart()` and `moveToEnd()` methods, respectively. These operations update the `order_column` for all affected items. ```php $tag->moveToStart(); $tag->moveToEnd(); ``` -------------------------------- ### Attach/Detach Tag with Type Source: https://github.com/spatie/laravel-tags/blob/main/docs/advanced-usage/using-types.md Shows how to attach or detach a tag instance that has a specific type to a model. This method works with tags previously created or found with a type. ```php $newsItem->attachTag($tagWithType); $newsItem->detachTag($tagWithType); ``` -------------------------------- ### Check for Model Tags and Retrieve by Type Source: https://github.com/spatie/laravel-tags/blob/main/README.md Check if a model possesses a specific tag and retrieve models that have tags of a particular type. ```PHP // checking if a model has a tag $newsItem->hasTag('first tag'); $newsItem->hasTag('first tag', 'some_type'); // Retrieve models that have tags of a specific type Model::withAnyTagsOfType('type'); Model::withAnyTagsOfType(['first type', 'second type']); ``` -------------------------------- ### Swap Tag Order Source: https://github.com/spatie/laravel-tags/blob/main/docs/advanced-usage/sorting-tags.md Exchanges the positions of two tag instances in the order using the `swapOrder()` method. This is useful for direct reordering between specific tags. ```php $tag->swapOrder($anotherTag); ``` -------------------------------- ### Retrieve models with any tags of any type (PHP) Source: https://github.com/spatie/laravel-tags/blob/main/docs/basic-usage/retrieving-tagged-models.md Returns models that have one or more of the given tags attached, without restricting the tags to a specific type if they are passed as a string. ```PHP //returns models that have one or more of the given tags of any type YourModel::withAnyTagsOfAnyType(['tag 1', 'tag 2'])->get(); ``` -------------------------------- ### Checking if a Model Has a Specific Tag Source: https://github.com/spatie/laravel-tags/blob/main/docs/basic-usage/using-tags.md Verify if an Eloquent model is associated with a particular tag, optionally specifying the tag type for more precise checks. ```php $yourModel->hasTag('tag 1'); // returns true if the model has the tag $yourModel->hasTag('non-existing tag'); // returns false if the model does not have the tag // Check with a specific type $yourModel->hasTag('tag 1', 'some_type'); // returns true if the model has the tag with the specified type $yourModel->hasTag('tag 1', 'non-existing type'); // returns false if the model does not have the tag with the specified type ``` -------------------------------- ### Eloquent Model Taggable Trait Source: https://github.com/spatie/laravel-tags/blob/main/docs/basic-usage/using-tags.md To make an Eloquent model taggable, add the `HasTags` trait to your model class. This enables tag management functionalities. ```php use Illuminate\Database\Eloquent\Model; use Spatie\Tags\HasTags; class YourModel extends Model { use HasTags; // ... } ``` -------------------------------- ### Query Tags by Translation (PHP) Source: https://github.com/spatie/laravel-tags/blob/main/docs/advanced-usage/adding-translations.md Shows how to query tags based on their translated names stored in a JSON column, using Laravel's query builder syntax for JSON fields. ```php \Spatie\Tags\Tag::query() ->where('name->fr', 'mon tag') ->first(); ``` -------------------------------- ### Retrieve models with any tags (PHP) Source: https://github.com/spatie/laravel-tags/blob/main/docs/basic-usage/retrieving-tagged-models.md Returns models that have one or more of the given tags attached. Optionally filters by tag type. If no type is provided, it looks for tags without a type. ```PHP YourModel::withAnyTags(['tag 1', 'tag 2'])->get(); //returns models that have one or more of the given tags that are typed `myType` YourModel::withAnyTags(['tag 1', 'tag 2'], 'myType')->get(); ``` -------------------------------- ### Retrieve Tags with Specific Type Source: https://github.com/spatie/laravel-tags/blob/main/README.md Fetch tags associated with a model, filtered by a specific tag type. ```PHP // retrieving tags with a type $newsItem->tagsWithType('categories'); $newsItem->tagsWithType('topics'); ``` -------------------------------- ### Retrieve models with all tags of any type (PHP) Source: https://github.com/spatie/laravel-tags/blob/main/docs/basic-usage/retrieving-tagged-models.md Returns only models that have all of the given tags attached, without restricting the tags to a specific type if they are passed as a string. Passing a non-existing tag will result in no models being returned. ```PHP //returns models that have all given tags of any type YourModel::withAllTagsOfAnyType(['tag 1', 'tag 2'])->get(); ``` -------------------------------- ### Finding Tags Containing Specific Values Source: https://github.com/spatie/laravel-tags/blob/main/docs/basic-usage/using-tags.md Retrieve tags that contain a specific substring in their name, ignoring case differences. Useful for searching or filtering tags. ```php use Spatie\Tags\Tag; Tag::findOrCreate('one'); Tag::findOrCreate('another-one'); Tag::findOrCreate('another-ONE-with-different-casing'); Tag::findOrCreate('two'); // Returns all tags except 'two', matching 'on' case-insensitively Tag::containing('on')->get(); ``` -------------------------------- ### Retrieve models with all tags (PHP) Source: https://github.com/spatie/laravel-tags/blob/main/docs/basic-usage/retrieving-tagged-models.md Returns only models that have all of the given tags attached. Optionally filters by tag type. Passing a non-existing tag or incorrect type will result in no models being returned. ```PHP //returns models that have all given tags that are not saved with a type YourModel::withAllTags(['tag 1', 'tag 2'])->get(); //returns models that have all given tags that are typed `myType` YourModel::withAllTags(['tag 1', 'tag 2'], 'myType')->get(); ``` -------------------------------- ### Detaching Tags from Eloquent Models Source: https://github.com/spatie/laravel-tags/blob/main/docs/basic-usage/using-tags.md Remove tags from an Eloquent model using their string representation or by providing a `Tag` model instance. Supports detaching single or multiple tags. ```php // Using a string $yourModel->detachTag('tag 1'); // Using an array of strings $yourModel->detachTags(['tag 2', 'tag 3']); // Using an instance of \Spatie\Tags\Tag $yourModel->detach(Spatie\Tags\Tag::find('tag4')); ``` -------------------------------- ### Manually Update Tag Order Source: https://github.com/spatie/laravel-tags/blob/main/docs/advanced-usage/sorting-tags.md Allows direct modification of the `order_column` for a tag instance and saving the changes to the database. This provides granular control over tag ordering. ```php $tag->order_column = 10; $tag->save(); ``` -------------------------------- ### Move Tag Position Source: https://github.com/spatie/laravel-tags/blob/main/docs/advanced-usage/sorting-tags.md Moves a specific tag instance up or down in the order using the `moveOrderUp()` and `moveOrderDown()` methods. These methods adjust the `order_column` accordingly. ```php $myModel->moveOrderUp(); $myModel->moveOrderDown(); ``` -------------------------------- ### Custom Tag Model for Default Locale Source: https://github.com/spatie/laravel-tags/blob/main/docs/advanced-usage/using-another-default-locale.md Defines a custom Tag model extending Spatie's Tag model to override the default locale for tag input, setting it to Dutch ('nl'). This allows tags to be managed in a specific language independent of the application's main locale. ```php namespace App\Models; use Spatie\Tags\Tag as SpatieTag; class YourTag extends SpatieTag { public static function getLocale(): string { return 'nl'; } } ``` -------------------------------- ### Set New Tag Order Source: https://github.com/spatie/laravel-tags/blob/main/docs/advanced-usage/sorting-tags.md Reorders tags by providing an array of tag IDs in the desired sequence using the `setNewOrder()` method. This method is part of the eloquent-sortable functionality. ```php Tag::setNewOrder($arrayWithTagIds); ``` -------------------------------- ### Override getTagClassName for Custom Tag Model Source: https://github.com/spatie/laravel-tags/blob/main/docs/advanced-usage/using-your-own-tag-model.md Demonstrates how to override the `getTagClassName` method in your Eloquent model to specify a custom tag model. This ensures the `HasTags` trait uses your custom model for tag relationships, extending `Spatie\Tags\Tag`. ```PHP use Illuminate\Database\Eloquent\Model; use Spatie\Tags\HasTags; class YourModel extends Model { use HasTags; public static function getTagClassName(): string { return YourTagModel::class; } } ``` -------------------------------- ### Override tags() Relationship for Custom Tag Model Source: https://github.com/spatie/laravel-tags/blob/main/docs/advanced-usage/using-your-own-tag-model.md Shows how to override the `tags()` relationship method to correctly define the morph-to-many relation. This specifies the custom tag class name obtained via `getTagClassName()` and ensures the correct foreign key (`tag_id`) is used for the relation. ```PHP use Illuminate\Database\Eloquent\Relations\MorphToMany; public function tags(): MorphToMany { return $this ->morphToMany(self::getTagClassName(), 'taggable', 'taggables', null, 'tag_id') ->orderBy('order_column'); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.