PHP code example of sourcedopen / tags

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

    

sourcedopen / tags example snippets


use Illuminate\Database\Eloquent\Model;
use SourcedOpen\Tags\Traits\HasTags;

class Post extends Model
{
    use HasTags;
}

use SourcedOpen\Tags\Models\Tag;

$tag = Tag::create([
    'name' => 'Laravel',
    'color' => '#FF2D20', // Optional hex color code
]);

// Attach a single tag
$post->attachTags($tag->id);

// Attach multiple tags
$post->attachTags([$tag1->id, $tag2->id]);

// Detach a single tag
$post->detachTags($tag->id);

// Detach multiple tags
$post->detachTags([$tag1->id, $tag2->id]);

// Sync tags (removes all existing and attaches the provided ones)
$post->syncTags([$tag1->id, $tag2->id]);

// Get all tags for a model
$tags = $post->tags;

// Query with tags
$posts = Post::whereHas('tags', function ($query) {
    $query->where('name', 'Laravel');
})->get();
bash
php artisan vendor:publish --tag="tags-migrations"
php artisan migrate