PHP code example of zaichaopan / taggable
1. Go to this page and download the library: Download zaichaopan/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/ */
zaichaopan / taggable example snippets
// tags table
Schema::create('tags', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->unique();
$table->string('slug')->unique();
$table->timestamps();
});
// taggables table
Schema::create('taggables', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('tag_id');
$table->foreign('tag_id')->references('id')->on('tags');
$table->morphs('taggable');
$table->timestamps();
});
// To add a create tag
use Zaichaopan\Taggable\Models\Tag;
Tag::create(['name' => 'laravel']);
// To get all the tag names
$names = Tag::getNames();
//
use Zaichaopan\Taggable\Traits\HasTags;
class Activity extends Model
{
use HasTags;
}
$tags = $activity->tags;
/**
*
* @param string|array ...$tagNames
*/
public function tag(...$tagNames): void
$activity->tag('outdoor');
// or
$activity->tag('outdoor', 'sports');
// or
$activity->tag(['outdoor', 'sports']);
/**
*
* @param string|array ...$tagNames
*/
public function reTag(...$tagNames): void
$activity->tag('outdoor');
// the tag of the activity will be sports
$activity->reTag('sports');
// or
$activity->reTag(['outdoor', 'sports']);
/**
*
* @param string|array ...$tagNames
*/
public function unTag(...$tagNames): void
$activity->unTag('outdoor');
// or
$activity->unTag('outdoor', 'sports');
// or
$activity->unTag(['outdoor', 'sports']);
public function unTagAll(): void
$activity->unTagAll();
bash
php artisan migrate