Multi Level Category.
Categorize is a category management for Laravel 4., (*1)
Installation
To get the lastest version of Theme simply require it in your composer.json file., (*2)
"teepluss/categorize": "dev-master"
You'll then need to run composer install to download it and have the autoloader updated., (*3)
Once Attach is installed you need to register the service provider with the application. Open up app/config/app.php and find the providers key., (*4)
'providers' => array(
'Teepluss\Categorize\CategorizeServiceProvider'
)
Publish config using artisan CLI., (*5)
php artisan config:publish teepluss/categorize
Migrate tables., (*6)
php artisan migrate --package=teepluss/categorize
Usage
Creating category as a root., (*7)
$categorize = Categorize::prepare(array(
'type' => 'Product',
'title' => 'Hardware',
'description' => 'Hardware is a ...'
));
$categorize->makeRoot();
Creating category as a child of parent., (*8)
$categorize = Categorize::prepare(array(
'type' => 'Product',
'title' => 'Hardware - CPU',
'description' => 'CPU is a ...'
));
$parent = Categorize::getCategoryProvider()->findById(1);
// or
// $parent = Categorize::getCategoryProvider()->findByName('Hardware');
$categorize->makeChildOf($parent);
Moving to a new root., (*9)
$category = Categorize::getCategoryProvider()->findByName('Hardware - CPU');
$category->makeRoot();
Moving to another parent., (*10)
$category = Categorize::getCategoryProvider()->findByName('Hardware - CPU');
$parent = Categorize::getCategoryProvider()->findByName('Software');
$category->makeChildOf($parent);
Update information., (*11)
$category = Categorize::getCategoryProvider()->findByName('Hardware - CPU');
$category->fill(array(
'title' => 'Software - Office',
'description' => 'Documenting'
));
$category->save();
Delete with children., (*12)
$category = Categorize::getCategoryProvider()->findByName('Hardware');
$category->deleteWithChildren();
List category as tree., (*13)
$category = Categorize::getCategoryProvider()->findByName('Hardware');
var_dump($category->getChildren()->toArray());
Build categories as tree., (*14)
$categories = Categorize::getCategoryProvider()->whereType('Blog')->get();
// Build only root categories.
$categories = Categorize::getCategoryProvider()->root()->whereType('Blog')->get();
var_dump(Categorize::tree($categories)->toArray());
Using categorize with model.
Define relation to model, (*15)
public function categories()
{
return $this->morphMany('Teepluss\Categorize\CategoryRelates\Relate', 'contentable');
}
Push content to a category., (*16)
$category = Categorize::getCategoryProvider()->findByName('Hardware - CPU');
$blog = Blog::find(24);
$blog->categories()->create(array('category_id' => $category->id));
List contents that belongs to a category., (*17)
$contentIds = Categorize::getCategoryProvider()->findById(1)->relates()->whereContentableType('Blog')->lists('contentable_id');
// or
// $contentIds = Categorize::getCategoryRelateProvider()->whereContentableType('Blog')->whereCategoryId(1)->lists('contentable_id');
$blogs = Blog::find($contentIds);
var_dump($blogs->toArray());
If you have some problem, Contact teepluss@gmail.com, (*18)
, (*19)