Convert Figma logo to code with AI

spatie logolaravel-tags

Add tags and taggable behaviour to your Laravel app

1,606
279
1,606
5

Top Related Projects

2,888

Laravel Stats Tracker

:heart: This package helps you to add user based follow system to your model.

Quick Overview

The spatie/laravel-tags package is a Laravel package that provides a simple and flexible way to add tags to your Eloquent models. It allows you to easily associate tags with your models, and provides a variety of features for managing and querying those tags.

Pros

  • Flexible: The package is designed to be highly flexible, allowing you to customize the behavior of tags to fit your specific needs.
  • Easy to Use: The package provides a straightforward and intuitive API for working with tags, making it easy to integrate into your Laravel application.
  • Efficient: The package uses a database-efficient approach to storing and querying tags, ensuring that your application remains performant even with a large number of tags.
  • Well-Documented: The package is well-documented, with detailed instructions and examples to help you get started.

Cons

  • Dependency on Laravel: The package is tightly integrated with the Laravel framework, which means that it may not be suitable for use in non-Laravel projects.
  • Limited Customization: While the package is highly flexible, there may be some edge cases where the built-in functionality is not sufficient, and you may need to extend or modify the package to meet your specific requirements.
  • Potential Performance Issues: Depending on the size and complexity of your application, the package's database-efficient approach may not be sufficient to handle very large tag datasets, and you may need to implement additional optimization strategies.
  • Potential Compatibility Issues: As with any third-party package, there is a risk of compatibility issues with other packages or with future versions of Laravel, which may require you to update or modify your code.

Code Examples

Here are a few examples of how you can use the spatie/laravel-tags package in your Laravel application:

  1. Attaching Tags to a Model:
$post = Post::create([
    'title' => 'My Blog Post',
    'content' => 'This is the content of my blog post.',
]);

$post->tags()->attach(['tag1', 'tag2', 'tag3']);
  1. Detaching Tags from a Model:
$post->tags()->detach(['tag2']);
  1. Querying Models by Tags:
$posts = Post::withAnyTags(['tag1', 'tag2'])->get();
$posts = Post::withAllTags(['tag1', 'tag2'])->get();
  1. Retrieving Tag Statistics:
$tagStats = Tag::withCount('models')->orderByCount('desc')->get();

Getting Started

To get started with the spatie/laravel-tags package, follow these steps:

  1. Install the package using Composer:
composer require spatie/laravel-tags
  1. Publish the package's configuration file and migration files:
php artisan vendor:publish --provider="Spatie\Tags\TagsServiceProvider"
  1. Run the database migrations to create the necessary tables:
php artisan migrate
  1. Add the Spatie\Tags\HasTags trait to any Eloquent models you want to be able to tag:
use Illuminate\Database\Eloquent\Model;
use Spatie\Tags\HasTags;

class Post extends Model
{
    use HasTags;
}
  1. Start using the package's API to manage tags for your models:
$post = Post::create([
    'title' => 'My Blog Post',
    'content' => 'This is the content of my blog post.',
]);

$post->tags()->attach(['tag1', 'tag2', 'tag3']);

That's it! You're now ready to start using the spatie/laravel-tags package in your Laravel application.

Competitor Comparisons

2,888

Laravel Stats Tracker

Pros of Tracker

  • Tracker provides a comprehensive set of features for tracking user activity, including page views, device information, and user agent data.
  • The package includes built-in support for various database drivers, making it easy to integrate with your existing database setup.
  • Tracker provides a user-friendly dashboard for visualizing and analyzing the collected data.

Cons of Tracker

  • Tracker may have a larger footprint and require more configuration compared to a simpler package like Laravel Tags.
  • The package may not be as actively maintained as some other Laravel packages, with the last major release being in 2018.
  • Tracker may have a steeper learning curve for developers who are not familiar with its features and architecture.

Code Comparison

Here's a brief comparison of the code for creating a new tag in Laravel Tags and Tracker:

Laravel Tags:

$tag = Tag::create([
    'name' => 'My Tag',
]);

Tracker:

$tracker = new \PragmaRX\Tracker\Tracker();
$tracker->pageViewed();

As you can see, the code for creating a new tag in Laravel Tags is more straightforward and concise, while Tracker requires a bit more setup and configuration to track page views.

:heart: This package helps you to add user based follow system to your model.

Pros of overtrue/laravel-follow

  • Provides a more comprehensive set of features for managing user relationships, including follow, unfollow, and block functionality.
  • Includes additional methods for retrieving followers, followings, and blocked users.
  • Supports multiple relationship types, allowing for more complex social interactions.

Cons of overtrue/laravel-follow

  • Slightly more complex to set up and configure compared to spatie/laravel-tags.
  • May have a steeper learning curve for developers unfamiliar with social relationship management.
  • Potentially less lightweight and focused than spatie/laravel-tags, which has a narrower scope.

Code Comparison

spatie/laravel-tags:

$post->tags()->attach([1, 2, 3]);
$post->tags()->detach([1, 2]);
$post->tags()->sync([1, 2, 3]);

overtrue/laravel-follow:

$user->follow($anotherUser);
$user->unfollow($anotherUser);
$user->toggleFollow($anotherUser);

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Add tags and taggable behaviour to a Laravel app

Latest Version on Packagist MIT Licensed GitHub Workflow Status Total Downloads

This package offers taggable behaviour for your models. After the package is installed the only thing you have to do is add the HasTags trait to an Eloquent model to make it taggable.

But we didn't stop with the regular tagging capabilities you find in every package. Laravel Tags comes with batteries included. Out of the box it has support for translating tags, multiple tag types and sorting capabilities.

You'll find the documentation on https://spatie.be/docs/laravel-tags.

Here are some code examples:

// apply HasTags trait to a model
use Illuminate\Database\Eloquent\Model;
use Spatie\Tags\HasTags;

class NewsItem extends Model
{
    use HasTags;
    
    // ...
}

// 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');

// get all tags of a model
$newsItem->tags;

// 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'); 

// retrieving tags with a type
$newsItem->tagsWithType('categories'); 
$newsItem->tagsWithType('topics'); 

// 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();

// 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

// 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"

// 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);

Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Requirements

This package requires Laravel 8 or higher, PHP 8 or higher, and a database that supports json fields and MySQL compatible functions.

Installation

You can install the package via composer:

composer require spatie/laravel-tags

The package will automatically register itself.

You can publish the migration with:

php artisan vendor:publish --provider="Spatie\Tags\TagsServiceProvider" --tag="tags-migrations"

After the migration has been published you can create the tags and taggables tables by running the migrations:

php artisan migrate

You can optionally publish the config file with:

php artisan vendor:publish --provider="Spatie\Tags\TagsServiceProvider" --tag="tags-config"

This is the contents of the published config file:

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, 
];

Documentation

You'll find the documentation on https://docs.spatie.be/laravel-tags/v4.

Find yourself stuck using the package? Found a bug? Do you have general questions or suggestions for improving the laravel-tags package? Feel free to create an issue on GitHub, we'll try to address it as soon as possible.

If you've found a bug regarding security please mail security@spatie.be instead of using the issue tracker.

Testing

  1. Copy phpunit.xml.dist to phpunit.xml and fill in your database credentials.
  2. Run composer test.

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you've found a bug regarding security please mail security@spatie.be instead of using the issue tracker.

Postcardware

You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium.

We publish all received postcards on our company website.

Credits

License

The MIT License (MIT). Please see License File for more information.