Laravel-Taxonomies

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

Github星跟踪图

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.

主要指标

概览
名称与所有者Lecturize/Laravel-Taxonomies
主编程语言PHP
编程语言PHP (语言数: 1)
平台
许可证MIT License
所有者活动
创建于2015-08-29 23:46:06
推送于2023-02-21 20:24:28
最后一次提交2023-02-21 21:24:26
发布数14
最新版本名称v1.2.0 (发布于 )
第一版名称v0.0.1 (发布于 )
用户参与
星数99
关注者数9
派生数30
提交数179
已启用问题?
问题数11
打开的问题数1
拉请求数3
打开的拉请求数0
关闭的拉请求数7
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?