teepluss/categorize

v0.1 2013-11-26 10:52 UTC

This package is not auto-updated.

Last update: 2024-09-23 15:06:35 UTC


README

Categorize 是 Laravel 4 的分类管理。

安装

要获取最新版本的主题,只需在您的 composer.json 文件中引入它。

"teepluss/categorize": "dev-master"

然后您需要运行 composer install 下载它并更新自动加载器。

一旦 Attach 安装完成,您需要将服务提供者注册到应用程序中。打开 app/config/app.php 并找到 providers 键。

'providers' => array(

    'Teepluss\Categorize\CategorizeServiceProvider'

)

使用 artisan CLI 发布配置。

php artisan config:publish teepluss/categorize

迁移表。

php artisan migrate --package=teepluss/categorize

用法

创建作为根的分类。

$categorize = Categorize::prepare(array(
    'type'        => 'Product',
    'title'       => 'Hardware',
    'description' => 'Hardware is a ...'
));

$categorize->makeRoot();

创建作为父分类子分类。

$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);

移动到新的根分类。

$category = Categorize::getCategoryProvider()->findByName('Hardware - CPU');

$category->makeRoot();

移动到另一个父分类。

$category = Categorize::getCategoryProvider()->findByName('Hardware - CPU');

$parent = Categorize::getCategoryProvider()->findByName('Software');

$category->makeChildOf($parent);

更新信息。

$category = Categorize::getCategoryProvider()->findByName('Hardware - CPU');
$category->fill(array(
    'title'       => 'Software - Office',
    'description' => 'Documenting'
));

$category->save();

删除带子分类的。

$category = Categorize::getCategoryProvider()->findByName('Hardware');

$category->deleteWithChildren();

以树状结构列出分类。

$category = Categorize::getCategoryProvider()->findByName('Hardware');

var_dump($category->getChildren()->toArray());

构建分类树。

$categories = Categorize::getCategoryProvider()->whereType('Blog')->get();

// Build only root categories.

$categories = Categorize::getCategoryProvider()->root()->whereType('Blog')->get();

var_dump(Categorize::tree($categories)->toArray());

使用模型与 categorize 一起使用。

定义与模型的关系

public function categories()
{
    return $this->morphMany('Teepluss\Categorize\CategoryRelates\Relate', 'contentable');
}

将内容推送到分类。

$category = Categorize::getCategoryProvider()->findByName('Hardware - CPU');

$blog = Blog::find(24);
$blog->categories()->create(array('category_id' => $category->id));

列出属于某个分类的内容。

$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());

支持或联系

如果您有任何问题,请联系 teepluss@gmail.com

Support via PayPal