Giter Site home page Giter Site logo

laravel-taxonomies's Introduction

Clone of lecturize/laravel-taxonomies

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 Roae\Taxonomies. The config file is now config/roae.php.

Installation

Require the package from your composer.json file

"require": {
	"roae/laravel-taxonomies": "dev-master"
}

and run $ composer update or both in one with $ composer require roae/laravel-taxonomies.

Next register the service provider and (optional) facade to your config/app.php file

'providers' => [
    // Illuminate Providers ...
    // App Providers ...
    Roae\Taxonomies\TaxonomiesServiceProvider::class
];

Configuration & Migration

$ php artisan vendor:publish --provider="Roae\Taxonomies\TaxonomiesServiceProvider"

This will create a config/roae.php and a migration file. In the config file you can customize the table names, finally you'll have to run migration like so:

$ php artisan migrate

Usage

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 for Roae in Vienna & Rome.

laravel-taxonomies's People

Contributors

alexanderpoellmann avatar roae avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.