Download the PHP package uwla/ltags without Composer
On this page you can find all versions of the php package uwla/ltags. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package ltags
Short Description Laravel Tagging System
License MIT
Homepage https://github.com/uwla/ltags
Informations about the package ltags
LTAGS
Tagging system for Laravel.
FEATURES
- arbitrary models: tags can be attached to any Eloquent Model.
- nested tags: a tag can also be tagged, allowing hierarchical structures.
- context aware: multiple tags with the same label can be created for different contexts.
- non intrusive: a resource can be tagged without any modification to its classes or DB tables.
- handy API: handy API to add/set/get/delete tags to a model, and to fetch models by their tags.
INSTALL
Install using composer:
Publish the ACL table migrations:
Run the migrations:
USAGE
This sections explains how to use the package. A demo app is also available on uwla/ltags-demo to illustrate use case.
Tags
Create tag:
Get tag:
Delete tag or tags by name:
To update a tag or tags, use Eloquent's update
method, as provided by
Laravel's documentation. This package provides alternative ways to create,
fetch and delete tags via their names just to make it more convenient and less
verbose, but when it comes to updating tags Laravel's interface is
straightforward and we cannot make it less verbose.
Tagged Models
Any model can be tagged. The only thing needed is to add the Taggable
trait:
We are going to use a Post model as an example of an application for user's posts, but it could be any model to be tagged including the user itself!
To add a tag or tags to a model:
To get a model's tags:
To check if a model has a tag or tags:
To remove a tag or tags from a model:
To set the tags of a model:
To get the models along with their tags or with the name (only) of the tags:
In the second line of each example, only the name of the tag is attached to the
model, not the tag itself (which is an instance of Tag
).
In the example above, each model will have a new attribute called tags
, which
is a Collection
of the model's directed tags (that is, nested tags are not
included). You can then apply other operations on top of the tags by using
Laravel's Collection handy methods, such as map
, pluck
, filter
, etc.
To get the models tagged by a tag, or tags:
In the example above, taggedBy
will give you all models that are tagged by at
least one of the provided tags. If you want the models to be tagged by all the
tags, then use taggedByAll
static method, which has the exact same syntax as
taggedBy
(in other words, the expected parameters are the same).
Notice: The taggedByAll
needs to check if all are matched, while
taggedBy
only needs to check one; thus, the taggedByAll
may be come slow if
you are using a relational database and are trying to fetch models tagged by
many tags with a high depth value. A graph database is more suitable in this
case. But for most applications this won't be a problem even if the depth
is,
let's say, equal to five or six.
Namespaces
There can be multiple tags with different namespaces (aka, contexts).
For example, the developer may want a "top" tag for posts and a "top" tag for videos (although I personally think it is good enough to have just one "top" tag for both posts and video as long as the developers take proper care).
Another example, there could be a tag "free" for the context of payments and a tag "free" in the context of Free Software, in which it means "freedom".
Ultimately, it is up to the developers to decide if namespaces
are needed,
since they are the ones who know the application requirements.
Let's see how to use them. To create, find or delete a tag for a particular namespace:
When the getTags
and hasTags
methods are called, they will use the
namespace obtained by calling getTagNamespace
, which by default is null
.
You could use a static
namespace for a given model or a dynamic one.
Here is an example of a static (aka, does not change) tag namespace:
Now, an example of a dynamic one:
Which you can then change in real time:
The namespace will only affect the methods to which were passed string names as arguments because the tags associated with those names needed to be fetched behind the scenes. If a Eloquent model or a Eloquent collection is passed as a argument, the namespace will have no effect because the Eloquent models already have the tag ids which uniquely identify the tags.
Custom Tag Model
You can use your custom variant for the Tag
model instead of the default one,
which is Uwla\Ltags\Models\Tag
.
In order for the Taggable
trait to use your tag instead of the default one,
you can override its method getTagClass
as follow:
Instead of doing that for every model, you could have your custom Taggable
trait as well:
Then, use App\Traits\Taggable
instead of Uwla\Ltags\Traits\Taggable
.
EXAMPLES
Besides using tags to organize and search content such as videos or articles, there are several ways in which tags can be quite handy. Let's explore them.
Suppose you have an applications with posts, some of them public. In your
Laravel PostPolicy
, you could do the following to check if a user is allowed
to view a particular post:
In the example above, we avoid having to add a is_public
column to the posts
table.
Another example is using tags to tag users based on their roles. Some
applications just add a role
column to the users table, then they would check
user->role
. But this creates an additional column. If you don't need a
sophisticated Access Control System, but need only a simple way to categorize
users based on roles, you could simply add tags to the user.
Other example is an application that promotes programming contests, which may have their visibility as public or private, their status as running or expired, and so on. Instead of adding several columns in the contests table, the developers may choose to use tags as an additional source of information about the contests. Of course, whether it is a good decision or not is up to the developers to figure out for their particular case.
These are just three simple examples. The possibilities are limited only by the developer's imagination. Tags can be adapted to any context which has resources that can be grouped by some criteria, any context which deals with clusters.
CONTRIBUTIONS
Contributions are welcome. Fork the repository, make your changes, then make a pull request. Check development for all development-related instructions.
HELP
Open an issue on this package repository on GitHub. I will be glad to help.
LICENSE
MIT.