PHP code example of mmanos / laravel-taggable

1. Go to this page and download the library: Download mmanos/laravel-taggable 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/ */

    

mmanos / laravel-taggable example snippets


use Mmanos\Taggable\Taggable;

class User extends Eloquent
{
	use Taggable;
}

class User extends Eloquent
{
	protected $tag_model = 'Tag';
	protected $taggable_table = 'user_tags';
}

class User extends Eloquent
{
	protected $taggable_table_sync = ['company_id', 'created_at', 'updated_at', 'deleted_at'];
}

$user->tag('Frequent Visitor');

$user->tag('Frequent Visitor', 'Happy');
// or
$user->tag(['Frequent Visitor', 'Happy']);

$user->untag('Frequent Visitor');

$user->untag('Frequent Visitor', 'Happy');
// or
$user->untag(['Frequent Visitor', 'Happy']);

if ($user->hasTag('Frequent Visitor')) {
	
}

$tags = $user->tags;

$tags = $user->tagsArray();

$users = User::withTag('Frequent Visitor')->take(10)->get();

$users = User::withTag('Frequent Visitor')
	->where('tag_created_at', '>', '2015-01-01 00:00:00')
	->with('company')
	->orderBy('tag_created_at', 'desc')
	->paginate(10);

$users = User::withTag('Frequent Visitor', 'Happy')->get();
// or
$users = User::withTag(['Frequent Visitor', 'Happy'])->get();

$users = User::withAnyTag('Frequent Visitor', 'Happy')->get();
// or
$users = User::withAnyTag(['Frequent Visitor', 'Happy'])->get();

// Fetch all users who have the 'Agent' tag and who have 'Frequent Visitor' or 'Happy'.
$users = User::withTag('Agent')->withAnyTag('Frequent Visitor', 'Happy')->get();

class User extends Eloquent
{
	public function tagContext()
	{
		return $this->company;
	}
}

class Tag extends Eloquent
{
	public static function applyQueryContext($query, $context)
	{
		$query->where('company_id', $context->id);
	}
	
	public static function applyModelContext($model, $context)
	{
		$model->company_id = $context->id;
	}
}

$users = User::withTag('Frequent Visitor')->withTagContext($company)->take(10)->get();
console
$ php artisan laravel-taggable:tags tags
console
$ php artisan laravel-taggable:taggable user_tags