#Taxonomy For Lumen
Originally this repo was forked from https://github.com/DevFactoryCH/taxonomy.
This package allows you to create vocabularies with terms in Laravel 4 and 5
In your composer.json
add:
"require": {
"rimantoro/taxonomy": "3.0.*"
}
"require": {
"rimantoro/taxonomy": "3.1.*"
}
From the terminal run
composer update
Then register the service provider and Facade by opening app/config/app.php
'Rimantoro\Taxonomy\TaxonomyServiceProvider',
'Taxonomy' => 'Rimantoro\Taxonomy\Facades\TaxonomyFacade',
Then run the following artisant command to publish the config and migrations:
php artisan vendor:publish
Then run the migrations:
php artisan migrate
And finally in any of the Models where you want to use the Taxonomy functionality, add the following trait:
<?php
class Car extends \Eloquent {
use \Rimantoro\Taxonomy\TaxonomyTrait;
}
In your composer.json
add:
"require": {
"rimantoro/taxonomy": "2.0.*"
}
From the terminal run
composer update
Then register the service provider and Facade by opening app/config/app.php
'Rimantoro\Taxonomy\TaxonomyServiceProvider',
'Taxonomy' => 'Rimantoro\Taxonomy\Facades\TaxonomyFacade',
If you want you can publish the config files if you want to change them
php artisan config:publish rimantoro/taxonomy
Perform the DB migrations to install the required tables
php artisan migrate --package=rimantoro/taxonomy
And finally in any of the Models where you want to use the Taxonomy functionality, add the following trait:
<?php
class Car extends \Eloquent {
use \Rimantoro\Taxonomy\TaxonomyTrait;
}
Creating a vocabulary:
Taxonomy::createVocabulary('Cars');
Retrieving a Vocabulary:
$vocabulary = Taxonomy::getVocabulary(1); // Using ID
$vocabulary = Taxonomy::getVocabularyByName('Cars'); // Using Name
Deleting a Vocabulary:
Taxonomy::deleteVocabulary(1); // Using ID
Taxonomy::deleteVocabularyByName('Cars'); // Using Name
Adding a Term to a vocabulary:
Taxonomy::createTerm($vocabulary->id, 'Audi');
You can also optionally specify a parent term and a weight for each, so you can group them together and keep them sorted:
$german_cars = Taxonomy::createTerm($vocabulary->id, 'German Cars');
$italian_cars = Taxonomy::createTerm($vocabulary->id, 'Italian Cars');
$term_audi = Taxonomy::CreateTerm($vocabulary->id, 'Audi', $german_cars->id, 0);
$term_bmw = Taxonomy::CreateTerm($vocabulary->id, 'BMW', $german_cars->id, 1);
$term_benz = Taxonomy::CreateTerm($vocabulary->id, 'Mercedes-Benz', $german_cars->id, 2);
$term_ferrari = Taxonomy::CreateTerm($vocabulary->id, 'Ferrari', $italian_cars->id, 0);
With the Car Model, I can create a new instance and assign it a term for the make it belongs to:
$car = Car::create([
'model' => 'A3',
]);
$car->addTerm($term_bmw->id);
$car->addTerm($term_benz->id);
$car->removeAllTerms(); // Remove all terms linked to this car
$car->addTerm($term_ferrari->id);
$car->removeTerm($term_ferrari-id); // Remove a specific term
$car->addTerm($term_audi->id);
// Get all the terms from the vocabulary 'Cars' That
// are attached to this Car.
$terms = $car->getTermsByVocabularyName('Cars');
To retrieve all the cars that match a given term:
$audis = Car::getAllByTermId($term_audi->id)->get();