PHP code example of wuwx / laravel-taxonomy
1. Go to this page and download the library: Download wuwx/laravel-taxonomy 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/ */
wuwx / laravel-taxonomy example snippets
use Wuwx\LaravelTaxonomy\Traits\HasTaxonomyTerms;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use HasTaxonomyTerms;
}
use Wuwx\LaravelTaxonomy\Models\Taxonomy;
$topics = Taxonomy::query()->create(['name' => 'Topics']);
$php = $topics->createTerm(['name' => 'PHP']);
$laravel = $topics->createTerm(['name' => 'Laravel'], parent: $php);
$post->attachTerm($php);
$post->attachTerms(['php', 'laravel'], taxonomy: 'topics');
Post::withAnyTerms(['php', 'laravel'], taxonomy: 'topics')->get();
$topics = Taxonomy::query()->create([
'name' => 'Topics',
'description' => 'Development topics',
'is_hierarchical' => true,
]);
$backend = $topics->createTerm(['name' => 'Backend']);
$php = $topics->createTerm(['name' => 'PHP'], parent: $backend);
$laravel = $topics->createTerm(['name' => 'Laravel'], parent: $php);
$topics->findTermBySlug('php');
$topics->rootTerms();
$post->attachTerm($php);
$post->attachTerm('laravel', taxonomy: 'topics');
$post->attachTerms([$php, $laravel]);
$post->syncTerms(['php', 'laravel'], taxonomy: 'topics');
$post->syncTerms(['php'], taxonomy: 'topics', detaching: false);
$post->detachTerm('laravel', taxonomy: 'topics');
$post->detachTerms(['php', 'laravel'], taxonomy: 'topics');
$post->detachAllTerms();
$post->attachTerm('laravel', taxonomy: 'topics');
$post->attachTerms(['php', 'laravel'], taxonomy: $topics);
$post->attachTerm($php, pivot: ['order' => 1, 'metadata' => json_encode(['primary' => true])]);
$post->attachTerms([$php, $laravel], pivot: ['order' => 5]);
$post->terms->first()->pivot->order; // 1
$post->terms->first()->pivot->metadata; // '{"primary":true}'
$post->hasTerm($php);
$post->hasTerm('laravel', taxonomy: 'topics');
$post->hasAnyTerms(['php', 'go'], taxonomy: 'topics');
$post->hasAllTerms(['php', 'laravel'], taxonomy: 'topics');
Post::whereHasTerm('laravel', taxonomy: 'topics')->get();
Post::withAnyTerms(['php', 'laravel'], taxonomy: 'topics')->get();
Post::withAllTerms(['php', 'laravel'], taxonomy: 'topics')->get();
Post::withoutTerms(['deprecated'], taxonomy: 'statuses')->get();
Post::withoutAnyTerms()->get();
Post::byTaxonomies([
'topics' => ['php', 'laravel'], // has php OR laravel
'cities' => ['shanghai'], // AND has shanghai
])->get();
$topics = Taxonomy::query()->create([
'name' => ['en' => 'Topics', 'zh' => '主题'],
'description' => ['en' => 'Blog topics', 'zh' => '博客主题'],
]);
$php = $topics->createTerm([
'name' => ['en' => 'PHP', 'zh' => 'PHP 编程'],
]);
app()->setLocale('zh');
$topics->name; // '主题'
$php->name; // 'PHP 编程'
$topics = Taxonomy::query()->create(['name' => 'Topics']);
$laravel->parent;
$php->children()->get();
$backend->descendants()->get();
$laravel->ancestors()->get();
$laravel->siblings()->get();
$backend->isRoot();
$laravel->isLeaf();
$backend->isAncestorOf($laravel);
$laravel->isDescendantOf($backend);
$backend->ancestors()->count(); // 0
$php->ancestors()->count(); // 1
$laravel->ancestors()->count(); // 2
$tree = $topics->toTree(); // nested with children relations
$flatTree = $topics->toFlatTree(); // flat list with computed depth attribute
use Wuwx\LaravelTaxonomy\Events\TermAttached;
Event::listen(TermAttached::class, function (TermAttached $event) {
// $event->model — the Eloquent model
// $event->termIds — array of attached term IDs
});
return [
'table_names' => [
'taxonomies' => 'taxonomies',
'terms' => 'taxonomy_terms',
'morph_pivot' => 'termables',
],
'models' => [
'taxonomy' => Taxonomy::class,
'term' => Term::class,
],
];
bash
php artisan vendor:publish --tag=laravel-taxonomy-config
php artisan vendor:publish --tag=laravel-taxonomy-migrations
php artisan migrate