Trexology / taxonomy by trexology
forked from DevFactoryCH/taxonomy

Create and manage a heirarchical taxonomy of terms within different vocabularies
36,411
5
3
Package Data
Maintainer Username: trexology
Maintainer Contact: trexlim@gmail.com (Trex Lim)
Package Create Date: 2016-04-01
Package Last Update: 2021-01-28
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2024-11-11 15:02:33
Package Statistics
Total Downloads: 36,411
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 5
Total Watchers: 3
Total Forks: 2
Total Open Issues: 1

Build Status Latest Stable Version Total Downloads License

#Taxonomy

This package allows you to create vocabularies with terms in Laravel 4 and 5

Installation

Laravel 5

In your composer.json add:

"require": {
	"trexology/taxonomy": "3.*"
}

From the terminal run

composer update

Then register the service provider and Facade by opening app/config/app.php

(Skip this step if you are on laravel 5.5)

'Trexology\Taxonomy\TaxonomyServiceProvider',

'Taxonomy'        => 'Trexology\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 \Trexology\Taxonomy\TaxonomyTrait;
}

Laravel 4

In your composer.json add:

"require": {
	"trexology/taxonomy": "2.0.*"
}

From the terminal run

composer update

Then register the service provider and Facade by opening app/config/app.php

'Trexology\Taxonomy\TaxonomyServiceProvider',

'Taxonomy' => 'Trexology\Taxonomy\Facades\TaxonomyFacade',

If you want you can publish the config files if you want to change them

php artisan config:publish trexology/taxonomy

Perform the DB migrations to install the required tables

php artisan migrate --package=trexology/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 \Trexology\Taxonomy\TaxonomyTrait;
}

Usage

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);

If terms contains additional params, it can be added in this manner:

$params = [
	'shortname' => "GC",
	'type' => "Premium Cars",
];
$german_cars = Taxonomy::CreateTerm($vocabulary->id, 'BMW', $german_cars->id, 0, $params)

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();

Add these lines to your routes file, to access the UI for managing terms/vocabulary

Route::resource('taxonomy', '\Trexology\Taxonomy\Controllers\TaxonomyController');
Route::post('postOrderTerms', '\Trexology\Taxonomy\Controllers\TaxonomyController@postOrderTerms');
Route::resource('terms', '\Trexology\Taxonomy\Controllers\TermsController');

Extend UI with your current admin theme by changing the configuration in taxonomy.config.php