PHP code example of kjmtrue / laravel-tags

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

    

kjmtrue / laravel-tags example snippets


// Create new tag by name
app('rinvex.tags.tag')->createByName('My New Tag');

// Create new tag by name, group, and translation
app('rinvex.tags.tag')->createByName('The very new tag', 'blog', 'en');

// Find first tag by name
app('rinvex.tags.tag')->firstByName('My New Tag');

// Find first tag by name, group, and translation
app('rinvex.tags.tag')->firstByName('وسم جديد', 'board', 'ar');

// Find tag(s) by name
app('rinvex.tags.tag')->findByName('My New Tag');

// Find tag(s) by name, group, and translation
app('rinvex.tags.tag')->findByName('وسم جديد', 'board', 'ar');

// Find multiple tags by names array
app('rinvex.tags.tag')->findByName(['Tag One', 'Tag Two']);

// Find multiple tags by delimited names (tag delimiter is customizable)
app('rinvex.tags.tag')->findByName('First Tag, Second Tag, Third Tag');

// Find tag(s) by name or create if not exists
app('rinvex.tags.tag')->findByNameOrCreate('My Brand New Tag');

// Find tag(s) by name, group, and translation or create if not exists
app('rinvex.tags.tag')->findByNameOrCreate(['My Brand New Tag 2', 'My Brand New Tag 3']);

// Get instance of your model
$post = new \App\Models\Post::find(1);

// Get attached tags collection
$post->tags;

// Get attached tags query builder
$post->tags();

// Single tag id
$post->attachTags(1);

// Multiple tag IDs array
$post->attachTags([1, 2, 5]);

// Multiple tag IDs collection
$post->attachTags(collect([1, 2, 5]));

// Single tag model instance
$tagInstance = app('rinvex.tags.tag')->first();
$post->attachTags($tagInstance);

// Single tag name (created if not exists)
$post->attachTags('A very new tag');

// Multiple delimited tag names (use existing, create not existing)
$post->attachTags('First Tag, Second Tag, Third Tag');

// Multiple tag names array (use existing, create not existing)
$post->attachTags(['First Tag', 'Second Tag']);

// Multiple tag names collection (use existing, create not existing)
$post->attachTags(collect(['First Tag', 'Second Tag']));

// Multiple tag model instances
$tagInstances = app('rinvex.tags.tag')->whereIn('id', [1, 2, 5])->get();
$post->attachTags($tagInstances);

// Single tag id
$post->hasAnyTags(1);

// Multiple tag IDs array
$post->hasAnyTags([1, 2, 5]);

// Multiple tag IDs collection
$post->hasAnyTags(collect([1, 2, 5]));

// Single tag model instance
$tagInstance = app('rinvex.tags.tag')->first();
$post->hasAnyTags($tagInstance);

// Single tag name
$post->hasAnyTags('A very new tag');

// Multiple delimited tag names
$post->hasAnyTags('First Tag, Second Tag, Third Tag');

// Multiple tag names array
$post->hasAnyTags(['First Tag', 'Second Tag']);

// Multiple tag names collection
$post->hasAnyTags(collect(['First Tag', 'Second Tag']));

// Multiple tag model instances
$tagInstances = app('rinvex.tags.tag')->whereIn('id', [1, 2, 5])->get();
$post->hasAnyTags($tagInstances);

app('rinvex.tags.tag')->create(['name' => ['en' => 'My New Tag'], 'slug' => 'custom-tag-slug']);

$tag = app('rinvex.tags.tag')->find(1);
$tag->entries(\App\Models\Post::class)->get();

// Single tag id
$post->withAnyTags(1)->get();

// Multiple tag IDs array
$post->withAnyTags([1, 2, 5])->get();

// Multiple tag IDs collection
$post->withAnyTags(collect([1, 2, 5]))->get();

// Single tag model instance
$tagInstance = app('rinvex.tags.tag')->first();
$post->withAnyTags($tagInstance)->get();

// Single tag name
$post->withAnyTags('A very new tag')->get();

// Multiple delimited tag names
$post->withAnyTags('First Tag, Second Tag, Third Tag');

// Multiple tag names array
$post->withAnyTags(['First Tag', 'Second Tag'])->get();

// Multiple tag names collection
$post->withAnyTags(collect(['First Tag', 'Second Tag']))->get();

// Multiple tag model instances
$tagInstances = app('rinvex.tags.tag')->whereIn('id', [1, 2, 5])->get();
$post->withAnyTags($tagInstances)->get();

$tag = app('rinvex.tags.tag')->find(1);

// Update name translations
$tag->setTranslation('name', 'en', 'New English Tag Name')->save();

// Alternatively you can use default eloquent update
$tag->update([
    'name' => [
        'en' => 'New Tag',
        'ar' => 'وسم جديد',
    ],
]);

// Get single tag translation
$tag->getTranslation('name', 'en');

// Get all tag translations
$tag->getTranslations('name');

// Get tag name in default locale
$tag->name;
shell
    php artisan rinvex:publish:tags
    
shell
    php artisan rinvex:migrate:tags