sohrab-az/laravel-taxonomy

A flexible taxonomy package for Laravel

Maintainers

Package info

github.com/sohrab-az/laravel-taxonomy

pkg:composer/sohrab-az/laravel-taxonomy

Statistics

Installs: 2

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

1.0.0 2026-05-03 13:52 UTC

This package is auto-updated.

Last update: 2026-05-03 13:54:56 UTC


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