PHP code example of bnqtoan / laravel-taxonomies

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

    

bnqtoan / laravel-taxonomies example snippets


"    "lecturize/laravel-taxonomies": "^1.2"
}



namespace App\Models;

use Lecturize\Taxonomies\Contracts\CanHaveCategories;
use Lecturize\Taxonomies\Traits\HasCategories;

class Post extends Model implements CanHaveCategories
{
    use HasCategories;

    // ...
}

$model->addCategory('My Category', 'blog_category')

$model->addCategories(['Add','Multiple','Categories'], 'blog_category')

$model->addCategory('My Category', 'blog_category', 1, 2)

$model->getCategories('taxonomy')

$model->getCategory('My Category', 'blog_category')

$model->hasCategory('My Category', 'blog_category')

$model->detachCategory('My Category', 'blog_category')

$model->detachCategories()

$model = Model::categorizedIn(['Add','Multiple','Categories'], 'blog_category')->get();

$model = Model::categorized('My Category', 'blog_category')->get();



namespace App\Models;

use App\Models\Post;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Lecturize\Taxonomies\Models\Taxonomy as TaxonomyBase;

class Taxonomy extends TaxonomyBase
{
    public function posts(): MorphToMany
    {
        return $this->morphedByMany(Post::class, 'taxable', 'taxables')
                    ->withTimestamps();
    }
}

$post = Post::find(1);

$post->addCategory('My First Category', 'blog_category');
$post->addCategories(['Category Two', 'Category Three'], 'blog_category');
bash
$ php artisan vendor:publish --provider="Cviebrock\EloquentSluggable\ServiceProvider"
$ php artisan vendor:publish --provider="Lecturize\Taxonomies\TaxonomiesServiceProvider"
bash
$ php artisan migrate