Download the PHP package cviebrock/eloquent-taggable without Composer

On this page you can find all versions of the php package cviebrock/eloquent-taggable. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package eloquent-taggable

Eloquent-Taggable

Easily add the ability to tag your Eloquent models in Laravel.

NOTE: These instructions are for the latest version of Laravel.
If you are using an older version, please install a version of the package that correlates to your Laravel version.

Build Status Total Downloads Latest Stable Version Latest Unstable Version SensioLabsInsight


Installation

Depending on your version of Laravel, you should install a different version of the package. NOTE: As of version 6.0, the package's version should match the Laravel version.

Laravel Version Package Version
^11.0 ^11.0
^10.0 ^10.0
9.0 ^9.0
8.0 ^8.0
7.0 ^7.0
6.0 ^6.0
5.8 3.5.*
5.7 3.4.*
5.6 3.3.*
5.5 3.2.*
5.4 3.1.*†

† Version 3.1 of the package requires PHP 7.0 or later, even though Laravel 5.4 doesn't.

Older versions of Laravel can use older versions of the package, although they are no longer supported or maintained. See CHANGELOG.md and UPGRADING.md for specifics, and be sure that you are reading the correct README.md for your version (GitHub displays the version in the master branch by default, which might not be what you want).

  1. Install the cviebrock/eloquent-taggable package via composer:

    The package will automatically register its service provider.

  2. Publish the configuration file:

  3. Publish the migrations:

If you modify the migrations, keep in mind that you can add more fields, but shouldn't remove any existing ones.

Also note that if you want to change the table names used for the package, this should be done in the configuration file (under the tables key).

  1. Finally, use artisan to run the migration to create the required tables:

    (Note that the migration file isn't published to your application, but will run anyway.)

Updating your Eloquent Models

Your models should use the Taggable trait:

NOTE: Make sure your model doesn't have an attribute and/or column in its database table called tags; the trait will add that attribute for you.

That's it ... your model is now "taggable"!

Usage

Adding and Removing Tags from a Model

Tag your models with the tag() method:

The tag() method is additive, so you can tag the model again and those tags will be added to the previous ones:

You can remove tags individually with untag() or entirely with detag():

You can also completely retag a model (a short form for detagging then tagging):

If you have an array of Tag model IDs (primary keys) already, you can tag a model using those IDs instead of the tag names:

Similarly, you can untag by ID as well:

Tagging/untagging/retagging by ID is useful if you have, for instance, a form with a multi-select dropdown or a list of checkboxes of all tags, e.g.:

When the form submits, the data sent to your controller is an array of all the selected tag IDs. It is then easy to update the model accordingly with the selected tags:

Working with a Model's Tags

You can get the array of all tags (technically, an Eloquent Collection):

You can also get the list of tags as a flattened array, or a delimited list:

Tag names are normalized (see below) so that duplicate tags aren't accidentally created:

You can also see if a model has a certain tag:

Query Scopes

For reference, imagine the following models have been tagged:

Model Id Tags
1 - no tags -
2 apple
3 apple, banana
4 apple, banana, cherry
5 cherry
6 apple, durian
7 banana, durian
8 apple, banana, durian

You can easily find models with tags through some query scopes:

And the inverse:

Some edge-case examples:

Combining scopes:

Finally, you can easily find all the tags used across all instances of a model:

Events

You can create a listener to handle when a model is tagged:

The listener receives the Cviebrock\EloquentTaggable\Events\ModelTagged event with the model and tags:

You can use also listen for the Cviebrock\EloquentTaggable\Events\ModelUntagged event which is fired when a tag is removed.

Other Methods

You can rename a tag for your model:

This will only affect instances of Model that were tagged "Apple". If another model was also tagged "Apple", those tags won't be renamed. (To rename a tag across all models, see the example below under the TagService Class.)

You can also get a list of popular tags for your model (including the model count):

You can also provide a minimum count (i.e., only return tags that have been used 3 or more times):

(Again, the above will limit the query to one particular model. To get a list of popular tag across all models, see the example below under the TagService Class.)

The Tag Model

There are a few methods you can run on the Tag model itself.

Tag::findByName('Apple') will return the Tag model for the given name. This can then be chained to find all the related models.

Under the hood, the above uses a byName() query scope on the Tag model, which you are also free to use if you want to write a custom query.

The TagService Class

You can also use TagService class directly, however almost all the functionality is exposed via the various methods provided by the trait, so you probably don't need to.

As always, take a look at the code for full documentation of the service class.

Configuration

Configuration is handled through the settings in /app/config/taggable.php. The default values are:

delimiters

These are the single-character strings that can delimit the list of tags passed to the tag() method. By default, it's just the comma, but you can change it to another character, or use multiple characters.

For example, if delimiters is set to ";,/", then this will work as expected:

glue

When building a string for the tagList attribute, this is the "glue" that is used to join tags. With the default values, in the above case:

normalizer

Each tag is "normalized" before being stored in the database. This is so that variations in the spelling or capitalization of tags don't generate duplicate tags. For example, we don't want three different tags in the following case:

Normalization happens by passing each tag name through a normalizer function. By default, this is PHP's mb_strtolower() function, but you can change this to any function or callable that takes a single string value and returns a string value. Some ideas:

You can access the normalized values of the tags through $model->tagListNormalized and $model->tagArrayNormalized, which work identically to $model->tagList and $model->tagArray (described above) except that they return the normalized values instead.

And you can, of course, access the normalized name directly from a tag:

connection

You can set this to specify that the Tag model should use a different database connection. Otherwise, it will use the default connection (i.e. from config('database.default')).

throwEmptyExceptions

Passing empty strings or arrays to any of the scope methods is an interesting situation. Logically, you can't get a list of models that have all or any of a list of tags ... if the list is empty!

By default, the throwEmptyExceptions is set to false. Passing an empty value to a query scope will "short-circuit" the query and return no models. This makes your application code cleaner, so you don't need to check for empty values before calling the scope.

However, if throwEmptyExceptions is set to true, then passing an empty value to the scope will throw a Cviebrock\EloquentTaggable\Exceptions\NoTagsSpecifiedException exception in these cases. You can then catch the exception in your application code and handle it however you like.

taggedModels

If you want to be able to find all the models that share a tag, you will need to define the inverse relations here. The array keys are the relation names you would use to access them (e.g. posts) and the values are the qualified class names of the models that are taggable (e.g. \App\Post). e.g. with the following configuration:

You will be able to do:

This will return a collection of all the Posts that are tagged "Apple".

model

By default, the package will use its own model class for Tags. If you want to use your own customized Tag model, then extend the package's class with your own class, and update the configuration to reference your model.

tables

By default, the package will create two tables to store the tag information. If you want to use different table names, then change these two values. The model, service, and migration classes will all read the configuration values.

Bugs, Suggestions, Contributions and Support

Thanks to everyone who has contributed to this project, with a big shout-out to Michael Riediger for helping optimize the SQL.

Please use GitHub for reporting bugs, and making comments or suggestions.

See CONTRIBUTING.md for how to contribute changes.

Copyright and License

eloquent-taggable was written by Colin Viebrock and is released under the MIT License.

Copyright (c) 2013 Colin Viebrock


All versions of eloquent-taggable with dependencies

PHP Build Version
Package Version
Requires php Version ^8.2
illuminate/config Version ^11.0
illuminate/database Version ^11.0
illuminate/support Version ^11.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package cviebrock/eloquent-taggable contains the following files

Loading the files please wait ....