PHP code example of foodineers / laravel-tag

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

    

foodineers / laravel-tag example snippets


'taggable_model' => App\Models\User::class,

use Foodineers\Tag\Traits\HasTags;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use HasTags;
}

$user->tags;                    // Collection of Tag models
$user->tags()->attach($tagId);  // Attach by ID
$user->tags()->detach($tagId);  // Detach by ID
$user->tags()->sync([1, 2, 3]); // Sync tag IDs

$user->tag('developer');
$user->tag('admin');

$user->detag('developer');

$tag = Tag::name('laravel');

$tag->tagged;           // Collection of tagged models (e.g. Users)
$tag->tagged()->get();  // Query builder

$tag->category;  // TagCategory or null

$category = TagCategory::name('programming');

$category->tags;  // Collection of Tag models

// Tag a user
$user->tag('developer');
$user->tag('admin');

// List tags
$user->tags->pluck('name');  // ['developer', 'admin']

// Remove a tag
$user->detag('admin');

// Sync tags (replace all)
$user->tags()->sync([
    Tag::name('moderator')->id,
    Tag::name('reviewer')->id,
]);

// From a tag: get all tagged models
$tag = Tag::name('developer');
$tag->tagged;  // Users (or your taggable model) with this tag
bash
php artisan vendor:publish --tag="tag-config"
php artisan vendor:publish --tag="tag-migrations"
php artisan migrate