Package Data | |
---|---|
Maintainer Username: | mayoz |
Maintainer Contact: | srcnckr@gmail.com (Sercan Çakır) |
Package Create Date: | 2017-07-25 |
Package Last Update: | 2017-09-11 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-08 03:05:47 |
Package Statistics | |
---|---|
Total Downloads: | 154 |
Monthly Downloads: | 0 |
Daily Downloads: | 0 |
Total Stars: | 4 |
Total Watchers: | 3 |
Total Forks: | 0 |
Total Open Issues: | 0 |
Easily add the ability to category your Eloquent models in Laravel 5.
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
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
.
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);
}
This package is licensed under The MIT License (MIT).