定制 datasdk/categories 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

datasdk/categories

最新稳定版本:1.0

Composer 安装命令:

composer require datasdk/categories

包简介

Categories model and traits for MyProject

README 文档

README

This package provides a Categories model and trait that can be used to attach categories to other Eloquent models through a polymorphic many-to-many relationship.

The category model supports nested set structure, translations, slugs, and tags through the traits and packages already used by the project.

Installation

composer require datasdk/categories

Using The Category Model

Import the model like this:

use MyProject\Categories\Models\Categories;

Example:

$category = Categories::create([
    'name' => ['da' => 'Nyheder', 'en' => 'News'],
    'description' => ['da' => 'Alle nyheder', 'en' => 'All news'],
    'type' => 'posts',
    'active' => true,
]);

Usage On A Model

Add the trait to a model that should support categories:

use MyProject\Categories\Traits\Categories;

class Post extends Model
{
    use Categories;
}

Relationships

categories()

Returns the model's categories through the categories_models table.

$post->categories;

entries($class)

Used from the category model to retrieve models of a specific class that are attached to the category.

$category->entries(Post::class)->get();

model()

Returns the polymorphic model relationship from the category model.

Trait Methods

setCategories($categoryIds)

Syncs a model with a list of category IDs.

$post->setCategories([1, 2, 3]);

setCategory($categoryIds)

Alias for setCategories().

$post->setCategory([1]);

attachCategory(...$categories)

Adds one or more categories without removing existing categories.

$post->attachCategory($category);
$post->attachCategory(1, 2, 3);

Query Scopes

withCategories(array $ids)

Returns models attached to one or more categories.

Post::withCategories([1, 2])->get();

withCategory($id)

Returns models attached to a single category.

Post::withCategory(1)->get();

Category Model Methods

getAllChildren($ids = null)

Returns all child category IDs for one or more categories. The method accepts both IDs and slugs.

$ids = Categories::getAllChildren([1, 5]);

addInclude(string $type, string $class)

Registers a model class as a category include type in the Laravel container.

Categories::addInclude('posts', Post::class);

When the request contains children=posts, the category can then retrieve related entries for that type.

Tags

The category model uses Spatie's HasTags trait. Use Spatie's tag methods, for example:

$category->syncTags(['featured', 'frontpage']);
$category->attachTag('important');
$category->tags;

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-05-12

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固