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?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?