Laravel Categorizable
, (*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)