sohrab-az / laravel-taxonomy
A flexible taxonomy package for Laravel
1.0.0
2026-05-03 13:52 UTC
Requires
- kalnoy/nestedset: ^7.0
- sohrab-az/laravel-language: ^1.0
Requires (Dev)
- orchestra/testbench: ^11.1
README
A flexible and powerful taxonomy system for Laravel supporting:
- Categories (hierarchical / multiple)
- Tags (flat / multiple)
- Brands (flat / single)
- Polymorphic relationships
- Nested set structure (tree support)
- Query builder & manager layer
๐ Features
- ๐ Attach multiple taxonomies to any model
- ๐ฒ Hierarchical taxonomy support (nested set)
- โ๏ธ Config-driven taxonomy types
- ๐ Fluent query builder
- ๐ง Smart sync / attach / detach logic
- ๐ Language-aware taxonomies
- ๐ค Creator tracking (polymorphic)
๐ฆ Installation
composer require sohrab-az/laravel-taxonomy
โ๏ธ Publish Config
php artisan vendor:publish --tag=taxonomy-config
๐งพ Configuration
config/taxonomy.php
return [ 'types' => [ 'category' => [ 'label' => 'Category', 'is_multiple' => true, 'is_hierarchical' => true, ], 'tag' => [ 'label' => 'Tag', 'is_multiple' => true, 'is_hierarchical' => false, ], 'brand' => [ 'label' => 'Brand', 'is_multiple' => false, 'is_hierarchical' => false, ], ], ];
๐ Database Structure
taxonomies table
- id
- name
- slug
- type
- order
- parent_id (nested set)
- _lft, _rgt (nested set)
- language_id
- created_by_type
- created_by_id
- timestamps
Pivot: taxonomyables
Polymorphic relation table:
- taxonomy_id
- taxonomyable_id
- taxonomyable_type
- timestamps
๐ง Usage
Add trait to your model
use SohrabAzinfar\Taxonomy\Traits\HasTaxonomies; class Post extends Model { use HasTaxonomies; }
๐ Relationship
$post->taxonomies();
โ๏ธ Attach Taxonomies
$post->attachTaxonomy($taxonomyId); $post->attachTaxonomy([1, 2, 3]);
โ Detach Taxonomies
$post->detachTaxonomy($taxonomyId); $post->detachTaxonomyByType('tag');
๐ Sync Taxonomies
$post->syncTaxonomy([1, 2, 3]); $post->syncTaxonomyByType('category', [1, 2]);
๐ Query Builder
$post->taxonomy()
Examples
$post->taxonomy() ->type('category') ->language('en') ->get();
Filters
->whereSlug('laravel') ->whereInSlug(['php', 'laravel']) ->whereId(1) ->children() ->root()
Execution
->get() ->first() ->pluck('name') ->count() ->exists()
๐ง Taxonomy Manager
Internal service handling logic:
app(TaxonomyManager::class)
Features:
- Type-aware syncing
- Handles multiple/single constraints
- Prevents duplicate attachments
- Grouped processing per taxonomy type
๐งฉ Taxonomy Type Helper
TaxonomyType::get('category'); TaxonomyType::exists('tag'); TaxonomyType::isMultiple('brand'); TaxonomyType::isHierarchical('category');
๐ Service Provider
Automatically registered via:
TaxonomyServiceProvider
Provides:
- Config merging
- Migration loading
- Config publishing
๐ Requirements
- PHP 8+
- Laravel 10+
- kalnoy/nestedset
##๐ License
MIT License