Package Data | |
---|---|
Maintainer Username: | AlexanderPoellmann |
Package Create Date: | 2015-08-29 |
Package Last Update: | 2023-02-21 |
Home Page: | |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-14 15:04:53 |
Package Statistics | |
---|---|
Total Downloads: | 9,516 |
Monthly Downloads: | 24 |
Daily Downloads: | 1 |
Total Stars: | 98 |
Total Watchers: | 10 |
Total Forks: | 30 |
Total Open Issues: | 1 |
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:
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.