aliziodev/laravel-terms 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

aliziodev/laravel-terms

Composer 安装命令:

composer require aliziodev/laravel-terms

包简介

Lightweight Laravel package for flat terms and polymorphic term relations.

README 文档

README

Tests Latest Version Total Downloads PHP Version License

Lightweight, flat taxonomy package for Laravel. Attach reusable terms — tags, categories, brands, colors, sizes, or any custom type — to any Eloquent model via a polymorphic pivot, without nested-set complexity.

Features

  • Flat term model — no nested sets, no adjacency lists; one simple terms table
  • Polymorphic pivot — attach terms to any model with a single trait
  • Type-based scoping — built-in enum types (tag, category, brand, color, size) plus arbitrary string types
  • Context on pivot — group term attachments by an optional context string (e.g. primary, sidebar), with context-aware sync and detach
  • Configurable morph keynumeric (default), uuid, or ulid
  • Auto slug generation — slugs derived from names, unique per type
  • hasTerm() — fast boolean existence check without loading the relation
  • whereHasTerms() — scope for AND / OR multi-term filtering
  • Term::ordered() — scope to order terms by order
  • Minimal surface area — manager, trait, one model, one migration

Requirements

Laravel PHP
11.x 8.2+
12.x 8.2+
13.x 8.3+

Installation

composer require aliziodev/laravel-terms

Run the installer (publishes config + migration, optionally migrates):

php artisan terms:install

Or publish manually:

php artisan vendor:publish --tag=terms-config
php artisan vendor:publish --tag=terms-migrations
php artisan migrate

Configuration

config/terms.php — published to your app:

return [
    'table_names' => [
        'terms'    => 'terms',
        'termables' => 'termables',
    ],

    // Supported: "numeric" (default), "uuid", "ulid"
    // Must match the primary key type of models using HasTerms.
    'morph_type' => 'numeric',

    // Built-in types. Arbitrary strings are also accepted at runtime.
    'types' => ['tag', 'category', 'brand', 'color', 'size'],

    // Swap in your own model if you need to extend Term.
    'model' => \Aliziodev\LaravelTerms\Models\Term::class,

    'slugs' => [
        'generate'            => true,
        'regenerate_on_update' => false,
    ],
];

Usage

1. Add the trait to your model

use Aliziodev\LaravelTerms\Traits\HasTerms;

class Product extends Model
{
    use HasTerms;
}

If your model uses UUID or ULID primary keys, set morph_type in config/terms.php to match.

2. Attach, sync, and detach terms

use Aliziodev\LaravelTerms\Enums\TermType;

// Sync — replaces all existing tags with the new list
$product->syncTerms(['new-arrival', 'sale'], TermType::Tag);

// Attach — adds without removing existing terms
$product->attachTerms(['nike'], TermType::Brand);

// Detach specific terms
$product->detachTerms(['sale'], TermType::Tag);

// Detach all terms of a type
$product->detachTerms([], TermType::Tag);

// Detach everything
$product->detachTerms();

// Detach within a specific context only (other contexts remain untouched)
$product->detachTerms([], TermType::Tag, 'homepage');
$product->detachTerms(['sale'], TermType::Tag, 'homepage');

Custom string types work without any configuration:

$product->syncTerms(['waterproof', 'breathable'], 'material');

3. Query

// All terms attached to the model
$product->terms;

// Terms of a specific type
$product->termsOfType(TermType::Tag)->get();

// Fast boolean existence check — no collection loaded
$product->hasTerm(TermType::Brand, 'nike');    // true / false

// Find products that have a specific term
Product::whereHasTerm(TermType::Brand, 'nike')->get();

// Models that have ALL of the given terms (AND — default)
Product::whereHasTerms(TermType::Tag, ['new', 'sale'])->get();

// Models that have ANY of the given terms (OR)
Product::whereHasTerms(TermType::Tag, ['new', 'sale'], 'or')->get();

4. Context

Attach the same term under different contexts (e.g. display slots):

$product->attachTerms(['red'], TermType::Color, 'primary');
$product->attachTerms(['blue'], TermType::Color, 'secondary');

// Syncing within a context leaves other contexts untouched
$product->syncTerms(['yellow'], TermType::Color, 'primary');

// Filter by context on the pivot
$product->termsOfType(TermType::Color)->wherePivot('context', 'primary')->get();

5. Facade

use Aliziodev\LaravelTerms\Facades\Terms;

$term  = Terms::findOrCreate('Summer', TermType::Tag);
$terms = Terms::findOrCreateMany(['new', 'sale'], TermType::Tag);

Terms::attach($product, ['red'], TermType::Color);
Terms::sync($product, ['blue'], TermType::Color);
Terms::detach($product, ['red'], TermType::Color);

6. Term model scopes

// Filter by type or slug
Term::query()->type('tag')->get();
Term::query()->slug('new-arrival')->first();

// Sort by order (asc by default)
Term::query()->type('tag')->ordered()->get();
Term::query()->ordered('desc')->get();

Extending the Term model

Publish the config and swap model:

// config/terms.php
'model' => App\Models\Term::class,
namespace App\Models;

class Term extends \Aliziodev\LaravelTerms\Models\Term
{
    // Add relations, scopes, or accessors here
}

Differences from laravel-taxonomy

Feature laravel-terms laravel-taxonomy
Hierarchy None (flat) Nested set / adjacency list
Term ordering order column Full tree ordering
Pivot context Yes Varies
Migration complexity 2 tables 2 tables
Footprint Minimal Feature-rich

Use laravel-terms when you need simple, flat labels. Use laravel-taxonomy when you need parent–child term trees.

Testing

composer test

License

MIT — see LICENSE.

aliziodev/laravel-terms 适用场景与选型建议

aliziodev/laravel-terms 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 159 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 04 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「category」 「taxonomy」 「tagging」 「laravel」 「terms」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 aliziodev/laravel-terms 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 aliziodev/laravel-terms 我们能提供哪些服务?
定制开发 / 二次开发

基于 aliziodev/laravel-terms 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-04-15