1. Go to this page and download the library: Download uwla/ltags library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
uwla / ltags example snippets
use Uwla\Ltags\Models\Tag;
// create single tag
$tag = Tag::createOne('foo'); // shorter way
$tag = Tag::create(['name' => 'foo']); // default way to create Eloquent models
// create multiple tags tag
$tags = Tag::createMany(['foo', 'bar', 'zoo']); // Eloquent is way more verbose
$tag = Tag::findByName('foo'); // get single tag
$tags = Tag::findByName(['foo', 'bar', 'zoo']); // get many tags
// delete a tag by name
Tag::delByName('foo'); // delete single tag
Tag::delByName(['foo', 'bar', 'zoo']); // delete multiple tags
// The method above only works for string and string arrays.
// If you have a Eloquent model or collection and want to delete it or them,
// do the following:
$model->delete();
$models->delete();
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Uwla\Ltags\Trait\Taggable
class Post extends Model
{
use Taggable; // just add this
// more code...
}
// add single tag, which is Eloquent
$tag = Tag::createOne('public');
$post->addTag($tag);
// add single tag by its name, no need to for you to fetch it first
$post->addTag('public');
// add tags
$tags = Tag::all();
$post->addTag($tags);
// add tags by name
$post->addTag(['php', 'laravel', 'composer']);
// add a tag or tags to multiple models at once
// (the second argument must be an Eloquent Collection of the model)
Post::addTagsTo($tags, $posts); // tags can be am Eloquent Collection
Post::addTagsTo(['html', 'css'], $posts); // tags can be an array of strings too
Post::addTagTo('php', $posts); // to pass a single tag, call addTagTo
// instead of addTagsTo (shorter syntax)
// get all tags
$tags = $post->getTags();
// you specify the depth of the search to get nested tags
// that is, if a post has a tag and that tag has another tag,
// you will get the parent tag as well.
$depth = 4;
$tags = $post->getTags($depth);
// get tags matching a regex
// (in this case, any tag whose name is made up of two words)
$tags = $post->getTagsMatching('/\W \W/');
// again, the depth of the search can be specified
$tags = $post->getTagsMatching('/\W \W/', $depth);
// check if has a single tag
$post->hasTag($tag); // Eloquent model
$post->hasTag('public'); // name string
// check if has all provided tags
$post->hasTags($tags); // Eloquent collection
$post->hasTags(['foo', 'bar']); // name string
// check if it has any of the provided tags
$post->hasAnyTags($tags); // Eloquent collection
$post->hasAnyTags(['foo', 'bar']); // name string
// the depth of the search can also be provided
$depth = 3;
$post->hasTag($tag, $depth);
$post->hasTags($tags, $depth);
$post->hasAnyTags($tags, $depth);
// remove single tag, which is Eloquent
$tag = Tag::createByName('public');
$post->delTag($tag);
// remove via tag name
$post->delTag('public');
// remove tags from Eloquent collection
$tags = $post->getTagsMatching('*www*');
$post->delTags($tags);
// remove tags by name
$post->delTags(['php', 'laravel', 'composer']);
// remove all tags
$post->delAllTags();
// remove a tag or tags from multiple models at once
// (the second argument must be an Eloquent Collection of the model)
Post::delTagsFrom($tags, $posts); // tags can be an Eloquent Collection
Post::delTagsFrom(['html', 'css'], $posts); // tags can be an array of strings
Post::delTagFrom('php', $posts); // to pass a single tag you can call
// delTagFrom instead of delTagsFrom
// remove all tags from the given models
Post::delAllTagFrom($posts);
// the set method basically removes all tags of the model
// and add the new ones, thus 'setting' its tags.
// It is syntax sugar.
$post->setTags($tags);
// attach the tags to the given posts
$posts = Post::withTags($posts);
$posts = Post::withTagNames($posts);
// all posts
$posts = Post::withTags(Post::all());
$posts = Post::withTagNames(Post::all());
// posts that match a condition
$posts = Post::withTags(Post::where($condition)->get());
$posts = Post::withTagNames(Post::where($condition)->get());
// by a single tag
$posts = Post::taggedBy($tag); // Eloquent model
$posts = Post::taggedBy('public'); // name string
// posts tagged by at least one of the given tags
$posts = Post::taggedBy($tags); // Eloquent collection
$posts = Post::taggedBy(['php', 'laravel']); // array of strings
// you can provide the depth of the search in the second argument
// default depth is 1
$posts = Post::taggedBy($tags, 3);
// it is possible to specify the namespace (explained in the next section)
$posts = Post::taggedBy($tags, 1, 'posts');
$posts = Post::taggedBy($tags, namespace: 'posts'); // named arguments syntax
// just add the namespace as the second parameter
$namespace = 'posts';
// create the tags
$tag = Tag::createOne($name, $namespace); // one tag
$tags = Tag::createMany($names, $namespace); // multiple tags
// find the tags
$tag = Tag::findByName($name, $namespace); // one tag
$tags = Tag::findByName($names, $namespace); // multiple tags
// delete the tags
Tag::delByName($name, $namespace); // one tag
Tag::delByName($names, $namespace); // multiple tags
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Uwla\Ltags\Trait\Taggable
class Post extends Model
{
use Taggable; // just add this
// override method
public function getTagNamespace()
{
return 'posts';
}
}
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Uwla\Ltags\Trait\Taggable
class Post extends Model
{
use Taggable; // just add this
public $tagNamespace = 'posts';
// override method
public function getTagNamespace()
{
return $this->tagNamespace;
}
}
$tag1 = Tag::createOne('public', 'bar'); // bar namespace
$tag2 = Tag::createOne('public', 'foo'); // foo namespace
$post->tagNamespace = 'bar'; // set namespace to 'bar'
$post->addTag('public'); // adds $tag1
$post->tagNamespace = 'foo'; // set namespace to 'foo'
$post->hasTag('public'); // returns false, since the namespace is 'foo'
$post->hasTag($tag); // returns true, since the model was provided
$post->addTag('public'); // adds $tag2
$post->tagNamespace = 'bar'; // set namespace to 'bar'
$post->delTag('public'); // deletes $tag1
$post->tagNamespace = 'foo'; // set namespace to 'foo'
$post->hasTag('public'); // returns true, since the namespace is 'foo'
namespace App\Models;
use Uwla\Ltags\Models\Tag as BaseTag;
class Tag extends BaseTag
{
// disable timestamps, if you want
$timestamps = false;
// maybe you changed the table name..
$table = 'tag';
}
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Uwla\Ltags\Trait\Taggable;
use App\Models\Tag;
class Post extends Model
{
use Taggable;
protected static function getTagClass()
{
return Tag::class;
}
}
namespace App\Traits;
use Uwla\Ltags\Trait\Taggable as BaseTaggable
use App\Models\Tag;
class Taggable extends BaseTaggable
{
protected static function getTagClass()
{
return Tag::class;
}
}
/**
* Determine whether the user can view the post
*
* @param App\User $user
* @param App\Post $post
* @return \Illuminate\Auth\Access\Response|bool
*/
public function view(User $user, Post $post)
{
// any user can view a public post
if ($post->hasTag('public'))
return true;
// user can view a private post if he is the post owner
// the "traditional" way is to put a `user_id` column in the posts table
// but of course there are better ways to do this.
return $post->user_id == $user->id;
}
// instead of
if ($user->role == 'admin')
{
// do stuff
}
// or maybe
if ($user->role == 'vip')
{
// allow vip content
}
// you could do the following
if ($user->hasTag('admin'))
{
// do stuff
}
// or maybe
if ($user->hasTag('vip'))
{
// allow vip content
}