2017 © Pedro Peláez
 

library laravel-categorizable

Polymorphic categorizable for Laravel

image

mayoz/laravel-categorizable

Polymorphic categorizable for Laravel

  • Tuesday, September 12, 2017
  • by mayoz
  • Repository
  • 1 Watchers
  • 2 Stars
  • 22 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 10 % Grown

The README.md

Laravel Categorizable

Latest Version on Packagist Software License Build Status StyleCI, (*1)

Easily add the ability to category your Eloquent models in Laravel 5., (*2)

Installation

You can install the package via composer:, (*3)

``` bash composer require mayoz/laravel-categorizable, (*4)


Register the service provider in your `config/app.php` configuration file: ```php 'providers' => [ ... Mayoz\Categorizable\CategorizableServiceProvider::class, ... ];

You can publish the migration with:, (*5)

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:, (*6)

php artisan migrate

Usage

Suppose, you have the Post model as follows:, (*7)

<?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:, (*8)

$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., (*9)

Remove the existing category association for the Post model:, (*10)

$post = Post::find(1);

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

return $post;

The post model is associated with categories ids of 1, 2 and 4., (*11)

Rearrange the category relationships for the Post model:, (*12)

$post = Post::find(1);

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

return $post;

The post model is associated with categories ids of 1 and 5., (*13)

Extending

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

<?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:, (*15)

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

This is the contents of the published config file:, (*16)

<?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., (*17)

/**
 * 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;, (*18)

/**
 * 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)., (*19)

The Versions

12/09 2017

dev-master

9999999-dev

Polymorphic categorizable for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sercan Çakır

laravel polymorphic categorizable

12/09 2017

1.0.1

1.0.1.0

Polymorphic categorizable for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sercan Çakır

laravel polymorphic categorizable

01/08 2017

1.0.0

1.0.0.0

Polymorphic categorizable for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sercan Çakır

laravel polymorphic categorizable