Giter VIP home page Giter VIP logo

laravel-categorizable's Introduction

Laravel Categorizable

Latest Version on Packagist Software License Build Status StyleCI

Easily add the ability to category your Eloquent models in Laravel 5.

Installation

You can install the package via composer:

composer require mayoz/laravel-categorizable

Register the service provider in your config/app.php configuration file:

'providers' => [
    ...
    Mayoz\Categorizable\CategorizableServiceProvider::class,
    ...
];

You can publish the migration with:

php artisan vendor:publish --provider="Mayoz\Categorizable\CategorizableServiceProvider" --tag="migrations"

The migration has been published you can create the categories and categorizable tables. You are feel free for added new fields that you need. After, run the migrations:

php artisan migrate

Usage

Suppose, you have the Post model as follows:

<?php

namespace App;

use Mayoz\Categorizable\Categorizable;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use Categorizable;
}

Associate new categories for the Post model:

$post = Post::find(1);

$post->categorize([1, 2, 3, 4, 5]);

return $post;

Now, the post model is associated with categories ids of 1, 2, 3, 4 and 5.

Remove the existing category association for the Post model:

$post = Post::find(1);

$post->uncategorize([3, 5]);

return $post;

The post model is associated with categories ids of 1, 2 and 4.

Rearrange the category relationships for the Post model:

$post = Post::find(1);

$post->recategorize([1, 5]);

return $post;

The post model is associated with categories ids of 1 and 5.

Extending

I suggest, you always extend the Category model to define your relationships directly. Create you own Category model:

<?php

namespace App;

use Mayoz\Categorizable\Category as BaseCategory;

class Category extends BaseCategory
{
    /**
     * Get all posts for the relation.
     *
     * @return \Illuminate\Database\Eloquent\Relations\MorphedByMany
     */
    public function posts()
    {
        return $this->categorized(Post::class);
    }
}

You publish the package config:

php artisan vendor:publish --provider="Mayoz\Categorizable\CategorizableServiceProvider" --tag="config"

This is the contents of the published config file:

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Model Namespace
    |--------------------------------------------------------------------------
    |
    | Change these values when you need to extend the default category model
    | or if the user model needs to be served in a different namespace.
    |
    */

    'category' => App\Category::class,

];

That is all. Now let's play for relationship query with the category.

/**
 * Respond the post
 *
 * @param  \App\Category  $category
 * @return \Illuminate\Http\Response
 */
public function index(Category $category)
{
    return $category->posts()->paginate(10);
}

If we did not extend the Category model, as had to use;

/**
 * Respond the post
 *
 * @param  \App\Category  $category
 * @return \Illuminate\Http\Response
 */
public function index(Category $category)
{
    return $category->categorize(Post::class)->paginate(10);
}

License

This package is licensed under The MIT License (MIT).

laravel-categorizable's People

Contributors

mayoz avatar mikerockett avatar

Stargazers

 avatar  avatar  avatar  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.