ehsanmoradi / categorizable
为 Laravel 的 Eloquent 模型实现分类系统。
2.1.0
2023-03-07 19:20 UTC
Requires
- php: ^7.2|^8.0
- illuminate/database: ^7.0|^8.0|^9.0|^10.0
- illuminate/support: ^7.0|^8.0|^9.0|^10.0
- kalnoy/nestedset: ^6.0
- spatie/laravel-sluggable: ^3.3
This package is auto-updated.
Last update: 2024-09-07 22:45:09 UTC
README
此包允许您对 Eloquent 模型进行分类。只需在模型中使用特性即可。
要求
- PHP 7.2+
- Laravel 8+
安装
composer require ehsanmoradi/categorizable
发布并运行迁移
php artisan vendor:publish --provider="EhsanMoradi\Categorizable\CategorizableServiceProvider"
php artisan migrate
Laravel Categorizable 包将被 Laravel 自动发现。如果没有,请手动在 config/app.php 的 providers 数组中注册该包。
'providers' => [ ... \EhsanMoradi\Categorizable\CategorizableServiceProvider::class, ],
设置模型 - 只需在模型中使用特性。
<?php namespace App; use Illuminate\Database\Eloquent\Model; use EhsanMoradi\Categorizable\Categorizable; class Post extends Model { use Categorizable; }
用法
首先,我们需要创建一些用于工作的分类。Laravel Categorizable 包依赖于另一个名为 laravel-nestedset 的包,该包负责创建、更新、删除和检索单个或嵌套分类。这里演示了如何创建分类并将一个分类作为另一个分类的子分类。但您也可以参考包的仓库以获取完整的文档。 https://github.com/lazychaser/laravel-nestedset
use App\Post; use EhsanMoradi\Categorizable\Category; // first we create a bunch of categories // create "BackEnd" category Category::create([ 'name' => 'BackEnd' ]); // create "PHP" category Category::create([ 'name' => 'PHP' ]); // create "FrontEnd" category Category::create([ 'name' => 'FrontEnd' ]); // create "Test" Category (alternative way) $test = new Category(); $test->name = 'Test'; $test->save(); // assign "PHP" as a child of "BackEnd" category $parent = Category::findByName('BackEnd'); $child = Category::findByName('PHP'); $parent->appendNode($child); // delete "Test" Category $testObj = Category::findByName('Test'); $testObj->delete(); // assuming that we have these variables $post = Post::first(); // 3 different ways of getting a category's instance $backendCategory = Category::findById(1); // 'BackEnd' $phpCategory = Category::findByName('PHP'); // 'PHP' $frontendCategory = Category::find(3); // 'FrontEnd'
将帖子附加到分类
$post->attachCategory($phpCategory);
从分类中移除帖子
$post->detachCategory($phpCategory);
将帖子附加到分类列表
$post->syncCategories([ $phpCategory, $backendCategory ]);
从所有分类中移除帖子
$post->syncCategories([]);
同步帖子的附加分类
$post->syncCategories([ $frontendCategory ]); // removes attached categories & adds the given categories
检查帖子是否附加到分类(布尔值)
// single use case $post->hasCategory($phpCategory); // multiple use case $post->hasCategory([ $phpCategory, $backendCategory ]); // return boolean
附加到帖子的分类列表(数组)
$post->categoriesList(); // return array [id => name]
附加到帖子的分类 ID 列表(数组)
$post->categoriesId(); // return array
获取给定分类的所有附加帖子(集合)
$categoryPosts = Category::find(1) ->entries(Post::class) ->get(); // return collection
关系
categories() 关系
$postWithCategories = Post::with('categories') ->get(); // you have access to categories() relationship in case you need eager loading
parent 关系
$category = Post::first()->categories()->first(); $category->parent; // return the category's parent if available
children 关系
$category = Post::first()->categories()->first(); $category->children; // return the category's children if any
ancestors 关系
$category = Post::first()->categories()->first(); $category->ancestors; // return the category's ancestors if any
descendants 关系
$category = Post::first()->categories()->first(); $category->descendants; // return the category's descendants if any
鸣谢
- Ehsan Moradi - ehsan-moradi@live.com