Skip to content

Simple, nestable Terms & Taxonomies (similar to WordPress) for Laravel 5.

License

Notifications You must be signed in to change notification settings

mm-sam/Laravel-Taxonomies

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

55 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Latest Stable Version Total Downloads License

Laravel Taxonomies

Simple, nestable Terms & Taxonomies (similar to WordPress) for Laravel 5.

Important Notice

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.

Installation

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,
];

Configuration & Migration

$ 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

Usage

First, add our HasTaxonomies trait to your model.

<?php namespace App\Models;

use Lecturize\Taxonomies\Traits\HasTaxonomies;

class Post extends Model
{
    use HasTaxonomies;

    // ...
}
?>
Add a Term
$model->addTerm('My Category', 'taxonomy')
Add multiple Terms
$model->addTerm(['Add','Multiple','Categories'], 'taxonomy')
Add a Term with optional parent (taxonomy) & order
$model->addTerm('My Category', 'taxonomy', 1, 2)
Get all Terms for a model by taxonomy
$model->getTerms('taxonomy')
Get a specific Term for a model by (optional) taxonomy
$model->getTerm('My Category', 'taxonomy')
Convenience method for getTerm()
$model->hasTerm($term, 'taxonomy')
Remove a Term from model by (optional) taxonomy
$model->removeTerm($term, 'taxonomy')
Remove all Terms from model
$model->removeAllTerms()
Scope models with multiple Terms
$model = Model::withTerms($terms, 'taxonomy')->get();
Scope models with one Term
$model = Model::withTerm($term, 'taxonomy')->get();

Example

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.

License

Licensed under MIT license.

Author

Handcrafted with love by Alexander Manfred Poellmann in Vienna & Rome.

About

Simple, nestable Terms & Taxonomies (similar to WordPress) for Laravel 5.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%