PHP code example of muan / laravel-tags

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

    

muan / laravel-tags example snippets


'providers' => [
    // ...
    Muan\Tags\Providers\TagsServiceProvider::class,
    // ...
],

// Use trait
use Muan\Tags\Traits\Taggable;
 
class Post extends Model
{
    use Taggable;
    
    // ...
}

// Create tag
$tag = Muan\Tags\Model\Tag::create([
    'title' => $title = 'Laravel',
    'slug' => str_slug($title)
]);

// Attach tag
$post = App\Post::find(1);
$post->tags()->attach($tag->id);

$tags = ['Laravel', 'Eloquent', 'Tags'];

foreach ($tags as $index => $tag) {
    $preparedTag = [
        'title' => $tag,
        'slug' => str_slug($tag)
    ];

    $tagId = Muan\Tags\Model\Tag::create($prepareTag)->id;
    $tags[$index] = $tagId;
}

$post = App\Post::find($id = 1);
$post->tags()->sync($tags);

$tags = App\Post::find(1)->tags;
bash
// Slug is generated automatically using str_slug()

php artisan tag:create "tag title"