Skip to main content
Back to packages
239 GitHub stars

aliziodev/laravel-taxonomy

View on GitHub

Description

Laravel package for managing taxonomies, categories, tags, and hierarchical data structures.

1. Create Your First Taxonomy

use Aliziodev\LaravelTaxonomy\Facades\Taxonomy;
use Aliziodev\LaravelTaxonomy\Enums\TaxonomyType;
 
// Create a category
$electronics = Taxonomy::create([
'name' => 'Electronics',
'type' => TaxonomyType::Category->value,
'description' => 'Electronic products and gadgets',
]);
 
// Create a subcategory
$smartphones = Taxonomy::create([
'name' => 'Smartphones',
'type' => TaxonomyType::Category->value,
'parent_id' => $electronics->id,
]);
 
// Create tags
$featured = Taxonomy::create([
'name' => 'Featured',
'type' => TaxonomyType::Tag->value,
]);

2. Associate with Models

// Assuming you have a Product model with HasTaxonomy trait
$product = Product::create([
'name' => 'iPhone 15 Pro',
'price' => 999.99,
]);
 
// Attach taxonomies
$product->attachTaxonomies([$electronics->id, $smartphones->id, $featured->id]);
 
// Or attach by slug
$product->attachTaxonomies(['electronics', 'smartphones', 'featured']);

3. Query and Filter

// Find products in electronics category
$products = Product::withTaxonomyType(TaxonomyType::Category)
->withTaxonomySlug('electronics')
->get();
 
// Get all taxonomies of a specific type
$categories = Taxonomy::findByType(TaxonomyType::Category);
 
// Get hierarchical tree
$categoryTree = Taxonomy::tree(TaxonomyType::Category);

Related Content on Laravel Daily

Video

Recent Courses on Laravel Daily

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.