laravelcity/laravel-categories

此包为您的模型提供可分类的行为。

1.0.2 2018-11-08 06:53 UTC

This package is auto-updated.

Last update: 2024-09-08 20:24:33 UTC


README

您可以为任何模型创建单独的 categories

安装 laravel categories

步骤1

在您的终端中运行以下语句

composer require laravelcity/laravel-categories

步骤2

config/app.php 中添加 providerfacade

    providers => [
      ...
      Laravelcity\Categories\CategoriesServiceProvider::class,
    ],


    aliases => [
      ...
      'Category'=> \Laravelcity\Categories\Facade\Category::class,
    ]

步骤3

php artisan vendor:publish --provider=Laravelcity\Categories\CategoriesServiceProviderphp artisan vendor:publish

现在您可以在 config/categories.php 文件中自定义配置

步骤4

php artisan migrate

如何管理每个模型的分类

例如,我们想为帖子创建分类


    class PostCategoriesController extends Model
    {
    
        protected $category;

        public function __construct()
        {
            // set model type for categories
            $this->category=Category::newCollection('Post'); 
        }
        
       // return all categories
       public function index()
       {
          $categories=$this->category->rootCategories();
          return view('view-name')->with(['categories'=>$categories]);
       }

        // insert category
        public function store(Request $request)
        {
            $request->validate(['title'=>'required','parent'=>'required']);

            try{
                 $this->category->insert($request);
                 return redirect()->back()->withSuccess('insert success');

            } catch (CategoryException $e){
                return redirect()->back()->withErrors($e->getMessage());
            }
        }

        // edit category
        public function edit($id)
        {
            try {
                $category=$this->category->find($category_id);
                .
                .
                .
                
            } catch (CategoryException $e){
                return redirect()->back()->withErrors($e->getMessage());
            }
        }

        // update category
        public function update(Request $request, $category_id)
        {
            try {
                $this->category->update($request,$category_id);
                .
                .
                .
              
            } catch (CategoryException $e){
                return redirect()->back()->withErrors($e->getMessage());
            }
        }

        // run actions to categories
        public function actions()
        {
            try {
                $this->category->runActions();
                .
                .
                .

            } catch (CategoryException $e){
                return redirect()->back()->withErrors($e->getMessage());
            }
        }

       // destroy category
        public function destroy($category_id)
        {
            try {
                $this->category->destroy($category_id);
                .
                .
                .

            }catch (CategoryException $e){
                return redirect()->back()->withErrors($e->getMessage());
            }
        }

       // return trashed categories
        public function trash()
        {
            try {
                $categories= $this->category->trash(); //return collection
                .
                .
                .

            }catch (CategoryException $e){
                return redirect()->back()->withErrors($e->getMessage());
            }
        }

    }
    

$category->runActions(); 它有三个操作符

  • 销毁
  • 删除
  • 恢复

要运行这些方法,发送以下参数

  • 动作

    这是一个保存所选操作符的参数。参数是字符串,仅 => 'destroy','delete','restore'

  • 选择

    这是一个保存要执行操作的所选分类的参数。参数是数组,例如 => [2,5,8,9]

其他方法

  • $this->category->rootCategories() 返回根分类

  • $this->category->htmlSelectList(['class'=>'form-control','name'=>'parent'],null,true) 返回分类的 html select

  • $this->category->htmlUlList(['class'=>'form-control','name'=>'parent']) 返回分类的 html ul(列表)

  • $this->category->all() 返回所有分类

  • $this->category->findModels(array $id) 返回模型列表数组

  • $this->category->arrayList() 返回分类的数组列表

  • $this->category->depth(Categories $category) 返回分类深度

如何使用分类

1- 创建一个名为 Categories 的模型并扩展 \Laravelcity\Categories\Models\Categories,并按以下方式建立关系。
    namespace App\Models;
        
    class Categories extends \Laravelcity\Categories\Models\Categories
    {
        public function posts()
        {
            return $this->morphedByMany(Post::class, 'categorieable');
        }
    }

以这种方式创建所有关系,只需更改名称(posts())和模型(Post::class)的关系即可

以下是一个示例


    namespace App\Models;

    class Post extends Model
    {
    
        public function categories()
        {
            return $this->morphToMany(App\Models\Categories::class, 'categorieable');
        }

    }
    
2- 为模型项目设置分类
    public function __construct(Post $post)
    {
        $this->category = Category::newCollection('Post','App\Models\Categories::class');
        // The second parameter is to identify the model that we created for the relationship
        $this->post = $post;
    }
    
    public function store(Request $request){
    
        $post=$this->post->create($request->except(['_token']));
        
        if(\request()->input('category_id',null)){
            $categories=$this->category->findModels(\request()->input('category_id'));
            // category_id must have an array
            $post->category()->sync($categories);
        }
         
    }
    
2- 获取帖子的分类
    public function show($request){
    
         $post=Post::find($id);
         $categories=$post->categories; // return Categories collect
        
    }
     
2- 获取分类的帖子

     // $category_id it can be `string` or `number` 
     $category=$this->category->find($category_id);
     return $category->posts; // return Categories collect
    

请注意,您可以使用不同模型创建多个分类