melsaka/categorist

为 Laravel 的 Eloquent 模型实现分类系统。

dev-main 2023-09-16 09:58 UTC

This package is auto-updated.

Last update: 2024-09-16 22:50:17 UTC


README

Categorist 是一个 Laravel 扩展包,旨在处理应用程序中各种模型的分层分类。它允许您将数据组织到分类中,并提供一个易于使用的界面来执行添加、编辑和查询分类等操作。

安装

使用 Composer 将包添加到您的 Laravel 应用程序中。

composer require melsaka/categorist

在 config/app.php 中注册包的服务提供者。

'providers' => [
    ...
    Melsaka\Categorist\CategoristServiceProvider::class,
    ...
];

运行迁移以将所需的表添加到数据库中。

php artisan migrate

Categorized 特性添加到您想要分类的模型中。

use Melsaka\Categorist\Categorized;

class Post extends Model
{
    use Categorized;
    
    // ...   
}

配置

为了配置包,发布其配置文件。

php artisan vendor:publish --tag=categorist

然后您可以根据需要修改配置文件以更改分类表名,默认为:categories

使用方法

添加新分类

您可以通过使用 Category::add() 方法为特定模型(如 Post)添加新分类。如果已存在具有相同 slugtype 的分类,则将 返回 它。以下是一个示例

use Melsaka\Categorist\Models\Category;

$data = [
    'name' => 'Foo',
    'slug' => 'foo',
    'children' => [
        [
            'name' => 'Bar',
            'slug' => 'bar',
            'children' => [
                [
                    'name' => 'Baz',
                    'slug' => 'baz',
                ],
            ],
        ],
    ],
];

$category = Category::add(Post::class, $data);

如果您不希望返回现有记录

$category = Category::add(Post::class, $data, false); // will throw duplicate entry error if record exists 

检索与特定分类相关的模型

您可以使用 categorized() 方法检索与特定分类相关的所有模型。

$posts = $category->categorized();

检查分类是否是新添加的

您可以使用 isNew() 方法检查分类是否是新添加的或已存在并返回。

$isNew = Category::isNew($category);

编辑分类

要编辑分类,请使用 Category::edit() 方法。传递分类实例和要更新的数据数组。

use Melsaka\Categorist\Models\Category;

$data = [
    'name' => 'New Category Name',
];

Category::edit($category, $data);

删除分类

您可以使用 Category::remove() 方法删除分类。传递分类实例或其 ID。

Category::remove($category);

与模型同步分类

要同步分类与模型,请使用 Category::syncWith() 方法。这将关联一个分类与一个模型。

Category::syncWith($post, $category);

检查模型是否有分类

您可以使用 Category::has() 方法检查模型是否具有特定分类。

$hasCategory = Category::has($post, $category);

列出分类

要列出特定类型的所有分类,您可以使用 Category::list() 方法。

$categories = Category::list(Post::class);

检索分类树

要检索特定类型的所有分类的分层列表,您可以使用 Category::treeList() 方法。

$categoryTree = Category::treeList(Post::class);

处理模型关系

您可以使用 Eloquent 关系检索与模型关联的分类。例如,要检索 Post 模型的分类

$postWithcategories = Post::with('categories')->get();

// or retrieve only the categories

$postCategories = $post->categories;

$postCategories = Category::ofThis($post);

$firstCategory = $post->firstCategory();

要检索模型的父级分类,您可以使用 parentCategories() 方法。

$parentCategories = $post->parentCategories();

要检索与模型关联的分类的分层列表,您可以使用 treeCategories() 方法。

$treeCategories = $post->treeCategories();

您可以使用 categoriesList() 方法检索与模型关联的分类名称列表。

$categoryNames = $post->categoriesList();

您可以使用以下方法向模型 attachdetachsync 多个分类

// Attach categories to a model
$post->attachCategories($category);

// Detach categories from a model
$post->detachCategories($category);

// Sync categories with a model
$post->syncCategories($category);

// Check if a model has specific categories
$post->hasCategories($cat, $category);

// Check if a model has all of the specified categories
$post->hasAllCategories($cat, $category);

其他方法

您还可以使用其他方法检索相关数据。

// Retrieve all models related to a category
$posts = $category->categorized()->get();

// Retrieve the parent of a category
$parent = Category::with('parent')->get();

// Retrieve the children of a category
$children = Category::with('children')->get();

// Retrieve the ancestors of a category
$ancestors = Category::with('ancestors')->get();

// Retrieve the descendants of a category
$descendants = Category::with('descendants')->get();

注意:请参阅 Nested Sets 扩展包以获取更多方法和详细信息。

许可证

本扩展包在 MIT 许可证(MIT)下发布。