This package adds tagging support to your Laravel application. You can configure it to attach tags to any of your existing Eloquent models.
Add this to your composer.json file, in the require object:
After that, run composer install to install the package.
Register the Mmanos\Taggable\TaggableServiceProvider in your app configuration file.
Tags Migration and Model
First you'll need to publish a tags table and a Tag model. This table will hold a summary of all tags created by your taggable models.
$ php artisan laravel-taggable:tags tags
Note: Modify the last parameter of this call to change the table/model name.
Note: You may publish as many tags tables as you need, if you want to keep the tags separate for different types of content, for example.
Next, publish a migration for each type of content you want to tag. You may tag as many types of content as you wish. For example, if you want to be able to tag both a users table and a blog_posts table, run this migration once for each table.
$ php artisan laravel-taggable:taggable user_tags
Once the migration has been created, simply run the migrate command.
Next, add the Taggable trait to each taggable model definition:
Sometimes you will want to have some of the same fields in your content table synced to the taggable table records. This will allow you to filter and sort by these attributes when querying the taggable table. Luckily this system will automatically sync any fields you define to the taggable table records any time there are changes.
To get started, modify the taggable migration file to include your additional fields.
Then, tell your model which fields it needs to sync:
Now every time you create or update a model, these fields will by synced to all taggable table records for the piece of content.
Syncing Deleted Content
This package will automatically delete all taggable table records for a piece of content when that piece of content is deleted.
If you are using the SoftDeletingTrait and you are syncing the deleted_at column to your taggable table records, this package will automatically soft-delete all taggable table records for a piece of content when that piece of content is deleted. If the content is restored, then the taggable table records are restored as well.
Note: Query performance can be reduced for these types of queries if your queries match thousands of records or more.
And you may combine multiple filters:
// Fetch all users who have the 'Agent' tag and who have 'Frequent Visitor' or 'Happy'.$users=User::withTag('Agent')->withAnyTag('Frequent Visitor', 'Happy')->get();
Sometimes you might want to associate your tags (summary table) records with some custom context for your application. For example, say you have a companies table and a users table and each user belongs to a company. And now you also want to associate each tag record with a company allowing you to fetch all tags used by each individual company. In order to do so, we have to tell this package to be aware of this company context and modify it's queries accordingly.
To get started, make sure you modify your tags migration to include any context fields (company_id, in this case). You might also need to update the unique index, if necessary.
Then modify your taggable model by adding a tagContext method: