PHPackages                             aliziodev/laravel-terms - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. aliziodev/laravel-terms

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

aliziodev/laravel-terms
=======================

Lightweight Laravel package for flat terms and polymorphic term relations.

v1.1.2(1mo ago)0158MITPHPPHP ^8.2

Since Apr 15Pushed 1mo agoCompare

[ Source](https://github.com/aliziodev/laravel-terms)[ Packagist](https://packagist.org/packages/aliziodev/laravel-terms)[ RSS](/packages/aliziodev-laravel-terms/feed)WikiDiscussions main Synced today

READMEChangelog (5)Dependencies (15)Versions (8)Used By (0)

Laravel Terms
=============

[](#laravel-terms)

[![Tests](https://github.com/aliziodev/laravel-terms/actions/workflows/tests.yml/badge.svg)](https://github.com/aliziodev/laravel-terms/actions/workflows/tests.yml)[![Latest Version](https://camo.githubusercontent.com/eb94db102bb590c6714925a31583d67c2374866de6347c0e9e908ce48b480350/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f616c697a696f6465762f6c61726176656c2d7465726d732e737667)](https://packagist.org/packages/aliziodev/laravel-terms)[![Total Downloads](https://camo.githubusercontent.com/07bf58f74e9ccd42075a0a645eedddeeccffd67f3bde6738a4d5d320c576cd3d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f616c697a696f6465762f6c61726176656c2d7465726d732e737667)](https://packagist.org/packages/aliziodev/laravel-terms)[![PHP Version](https://camo.githubusercontent.com/831d1ae995fb95c17db78efd50eabe762471ecef25577a79ddbe41363aa8b920/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f616c697a696f6465762f6c61726176656c2d7465726d732e737667)](https://packagist.org/packages/aliziodev/laravel-terms)[![License](https://camo.githubusercontent.com/3b60fde0704c000b778b79f593015a4c2cb2e4006d131d6496a5ae4fd816b945/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f616c697a696f6465762f6c61726176656c2d7465726d732e737667)](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
--------

[](#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 key** — `numeric` (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
------------

[](#requirements)

LaravelPHP11.x8.2+12.x8.2+13.x8.3+Installation
------------

[](#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
-------------

[](#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
-----

[](#usage)

### 1. Add the trait to your model

[](#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

[](#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

[](#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

[](#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

[](#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

[](#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
------------------------

[](#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
---------------------------------

[](#differences-from-laravel-taxonomy)

Featurelaravel-termslaravel-taxonomyHierarchyNone (flat)Nested set / adjacency listTerm ordering`order` columnFull tree orderingPivot contextYesVariesMigration complexity2 tables2 tablesFootprintMinimalFeature-richUse **laravel-terms** when you need simple, flat labels. Use [laravel-taxonomy](https://github.com/aliziodev/laravel-taxonomy) when you need parent–child term trees.

Testing
-------

[](#testing)

```
composer test
```

License
-------

[](#license)

MIT — see [LICENSE](LICENSE).

###  Health Score

42

—

FairBetter than 88% of packages

Maintenance89

Actively maintained with recent releases

Popularity13

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~76 days

Recently: every ~110 days

Total

7

Last Release

56d ago

PHP version history (2 changes)1.0.0PHP ^8.1

v1.0.0PHP ^8.2

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/187039973?v=4)[Alizio](/maintainers/aliziodev)[@aliziodev](https://github.com/aliziodev)

---

Top Contributors

[![mu-hanz](https://avatars.githubusercontent.com/u/44943686?v=4)](https://github.com/mu-hanz "mu-hanz (11 commits)")

---

Tags

laraveltaggingcategorytaxonomyterms

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

Type Coverage Yes

### Embed Badge

![Health badge](/badges/aliziodev-laravel-terms/health.svg)

```
[![Health](https://phpackages.com/badges/aliziodev-laravel-terms/health.svg)](https://phpackages.com/packages/aliziodev-laravel-terms)
```

###  Alternatives

[psalm/plugin-laravel

Psalm plugin for Laravel

3355.3M345](/packages/psalm-plugin-laravel)[laravel/ai

The official AI SDK for Laravel.

1.0k3.2M194](/packages/laravel-ai)[api-platform/laravel

API Platform support for Laravel

58171.5k14](/packages/api-platform-laravel)[pressbooks/pressbooks

Pressbooks is an open source book publishing tool built on a WordPress multisite platform. Pressbooks outputs books in multiple formats, including PDF, EPUB, web, and a variety of XML flavours, using a theming/templating system, driven by CSS.

45444.2k1](/packages/pressbooks-pressbooks)[simplestats-io/laravel-client

Server-side analytics for Laravel that follows the full funnel from visit to registration to payment, attributed to the channel that drove it. Revenue, MRR, churn and ad-spend profit (ROAS/CAC) per channel. GDPR compliant, ad-blocker proof.

5021.9k](/packages/simplestats-io-laravel-client)[aliziodev/laravel-taxonomy

Laravel Taxonomy is a flexible and powerful package for managing taxonomies, categories, tags, and hierarchical structures in Laravel applications. Features nested-set support for optimal query performance on hierarchical data structures.

24426.4k](/packages/aliziodev-laravel-taxonomy)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
