Simple, nestable Terms & Taxonomies (similar to WordPress) for Laravel 5.
This package is a work in progress, please use with care and feel free to report any issues or ideas you may have!
We've transferred this package to a new owner and therefor updated the namespaces to Lecturize\Taxonomies. The config file is now config/lecturize.php
.
Require the package from your composer.json
file
"require": {
"lecturize/laravel-taxonomies": "dev-master"
}
and run $ composer update
or both in one with $ composer require lecturize/laravel-taxonomies
.
Next register the service provider and (optional) facade to your config/app.php
file
'providers' => [
// ...
Cviebrock\EloquentSluggable\ServiceProvider::class,
Lecturize\Taxonomies\TaxonomiesServiceProvider::class,
];
$ php artisan vendor:publish --provider="Cviebrock\EloquentSluggable\ServiceProvider"
$ php artisan vendor:publish --provider="Lecturize\Taxonomies\TaxonomiesServiceProvider"
This will create a config/sluggable.php
, a config/lecturize.php
and a migration file, that you'll have to run like so:
$ php artisan migrate
First, add our HasTaxonomies
trait to your model.
<?php namespace App\Models;
use Lecturize\Taxonomies\Traits\HasTaxonomies;
class Post extends Model
{
use HasTaxonomies;
// ...
}
?>
$model->addTerm('My Category', 'taxonomy')
$model->addTerm(['Add','Multiple','Categories'], 'taxonomy')
$model->addTerm('My Category', 'taxonomy', 1, 2)
$model->getTerms('taxonomy')
$model->getTerm('My Category', 'taxonomy')
$model->hasTerm($term, 'taxonomy')
$model->removeTerm($term, 'taxonomy')
$model->removeAllTerms()
$model = Model::withTerms($terms, 'taxonomy')->get();
$model = Model::withTerm($term, 'taxonomy')->get();
Add categories to an Eloquent model
$post = Post::find(1);
$post->addTerm('My First Category', 'category');
$post->addTerm(['Category Two', 'Category Three'], 'category');
First fo all, this snippet will create three entries in your terms
table, if they don't already exist:
- My First Category
- Category Two
- Category Three
Then it will create three entries in your taxonomies
table, relating the terms with the given taxonomy "category".
And last it will relate the entries from your taxonomies
table with your model (in this example a "Post" model) in your pivot
table.
Why three tables?
Imagine you have a Taxonomy called post_cat and another one product_cat, the first categorises your blog posts, the second the products in your online shop. Now you add a product to a category (a term) called Shoes using $product->addTerm('Sheos', 'product_cat');
. Afterwards you want to blog about that product and add that post to a post_cat called Shoes as well, using $product->addTerm('Sheos', 'post_cat');
.
Normally you would have two entries now in your database, one like ['Sheos','product_cat']
and another ['Sheos','post_at']
. Oops, now you recognize you misspelled Shoes, now you would have to change it twice, for each Taxonomy.
So I wanted to keep my Terms unique throughout my app, which is why I separated them from the Taxonomies and simply related them.
Licensed under MIT license.
Handcrafted with love by Alexander Manfred Poellmann in Vienna & Rome.