NewWe open-sourced 50+ Laravel packages
Custom AI apps, agents and automation — Roundly ConsultingRoundly
All packages
Tags for Laravel

Managing tags

Tags::type($type) is the catalog of one type. Every write goes through it — the Tag model carries reads only — so slugs, paths, usage counts and events stay right under concurrent writers.

Creating and seeding

use RoundlyConsulting\Tags\DataTransferObjects\TagData;

$genres = Tags::type('genre');

// The builder…
$music = $genres->make(['en' => 'Music', 'sk' => 'Hudba'])->color('#f24e29')->create();
$jazz = $genres->make('Jazz')->under($music)->position(0)->create();

// …or the DTO — the same code path
$rock = $genres->create(new TagData(name: 'Rock', parent: $music, icon: 'guitar'));

// Idempotent seeding: an existing tag is returned unchanged (never moved, recoloured or renamed),
// a missing one is created with every attribute — so the seeder may run again
$bebop = $genres->make('Bebop')->under($jazz)->color('#a7c4d6')->findOrCreate();
$live = Tags::type('label')->findOrCreate('Live');            // a name, a locale map or a TagData

create() refuses a name a live tag of the type and scope holds in any locale (DuplicateTagException) and restores a deleted tag that holds it with the new attributes, where it was unless you give a parent (TagRestored instead of TagCreated). findOrCreate() looks the name up in the scope, else globally, and returns a found tag unchanged — a deleted one comes back as it was, moved only by a given parent (see Names, slugs and translations); a locale map is matched on the current locale’s entry (else its first), its other entries only fill a newly created tag. It is allowed on curated types — it is an explicit catalog call — and two racing calls end with one tag.

Finding

$genres = Tags::type('genre');

$genres->find('jazz');              // a live tag by slug or name (along the locale chain), a key or a model
$genres->find(12);                  // by primary key (int)
$genres->find(TagKey::of($uuid));   // by any key — int, uuid or ulid
$genres->findOrFail('jazz');        // TagNotFoundException when missing
$genres->findMany($ids);            // by keys, in the order given — malformed, foreign or missing keys skipped
$genres->findTrashed('jazz');       // a deleted tag of exactly this handle's scope, to restore it

$genres->all();                     // every live tag of the type, by position
$genres->roots();                   // the roots (every tag of a flat type)
$genres->query()->where('usage_count', '>', 10)->get();   // Builder<Tag> of the readable scopes

Tags::find($key);                   // a live tag of any type by primary key
Tags::findOrFail($key);

Editing

use RoundlyConsulting\Tags\DataTransferObjects\UpdateTagData;

// Fluently — only what you call changes
Tags::type('genre')->edit($jazz)->name('Jazz music')->color(null)->save();

// A rename of the current locale, or of several locales at once
Tags::type('genre')->rename($jazz, 'Jazz music');
Tags::type('genre')->rename($jazz, ['en' => 'Jazz music', 'sk' => 'Jazzová hudba']);

// The DTO — fields left out stay; null clears a description, colour or icon
Tags::type('genre')->update($jazz, new UpdateTagData(description: null, icon: 'sax'));

// A null (or blank) description entry clears that locale only
Tags::type('genre')->edit($jazz)->description(['sk' => null])->save();

A name or description map merges into the stored one per locale; a blank or null description entry clears that locale only. A rename onto a slug another tag holds is refused — and points to restore() when a deleted tag holds it. TagUpdated fires with the attributes that changed (name, description, slug, color, icon); a call that changes nothing fires nothing.

Deleting and restoring

use RoundlyConsulting\Tags\Enums\TagChildren;

$genres = Tags::type('genre');

$genres->delete($rock);                                   // soft-deletes it and detaches it everywhere
$genres->delete($music);                                  // TagHasChildrenException while it has live children
$genres->delete($music, TagChildren::Reparent);           // children move up to its parent
$genres->delete($music, TagChildren::Delete);             // the whole subtree goes

$genres->restore($rock);                                  // back, with no old taggings
$genres->restore($genres->findTrashed('rock'));           // find it by name first

delete() soft-deletes the tag and detaches it from every model in the same transaction, so a re-created tag of the same name never relabels old records. No per-model Untagged fires — TagDeleted carries the number of taggings removed. Deleting an already deleted tag, or restoring a live one, returns it unchanged and fires nothing. restore() refuses a child whose parent is still deleted, recomputes its path from the parent’s current place — where it must fit the type’s depth cap, its live subtree included — and keeps its position under an unchanged parent; restoring a parent restores only that tag.

Merging tags

use RoundlyConsulting\Tags\Enums\TagChildren;

$merge = $labels->merge($rockMusic, $rock);                    // every "Rock music" record now carries "Rock"
$genres->merge($jazz, $music, TagChildren::Reparent);          // Jazz's children move under Music
$labels->within($team)->merge($teamLabel, $globalLabel);       // a team's own tag into a shared one

$merge->moved;          // taggings moved to $rock
$merge->deduplicated;   // records that carried both: their "Rock music" tagging was removed
$merge->into;           // $rock, re-read, usage_count recounted
$merge->from;           // $rockMusic, now deleted

merge(Tag $from, Tag $into, TagChildren $children = TagChildren::Refuse) moves every tagging of $from to $into, then deletes $from — in one transaction — and returns a TagMerge.

  • Every tagging moves, whatever the model and whichever scope it belongs to: a tag shared by several teams’ records moves them all. The pivot row itself is moved, so its own columns (created_at, any column you added) stay. A record that carried both keeps its $into tagging and loses the $from one: it ends with exactly one.
  • $into’s usage_count is recounted from the pivot after the move, not added to, so a drifted count comes out exact; $from ends at 0.
  • Children of $from in a tree type: Refuse (the default) throws TagHasChildrenException while it has live children; Reparent moves them under $into — appended after its children, their subtrees’ paths and depths rewritten, the type’s depth cap checked as for a move; Delete deletes the subtree as delete() does (detached everywhere, counts zeroed). A deleted child stays with $from.
  • $from is soft-deleted the way delete() deletes it. restore() brings the tag back without its old taggings: they now belong to $into, and a restore never moves them back.
  • $from must be in the handle’s write scope (a team handle never deletes a global tag) and $into in a scope the handle reads — so within($team) merges a team tag into a global one. Children never move into another scope: Reparent across scopes throws InvalidTagMergeException.
  • Refused, changing nothing: the same tag twice or $into inside $from’s subtree (InvalidTagMergeException), a tag of another type (TagTypeMismatchException), a tag outside the handle’s scopes (TagScopeMismatchException) and a deleted tag (TagNotFoundException, “The tag is deleted.”).
  • A merge fires TagMerged only, once, after commit — no TagDeleted for $from or its subtree, no TagMoved for a reparented child, no per-model Tagged or Untagged (see Events).
  • An attach of $from racing the merge either commits first and is moved, or is refused once $from is deleted — it never leaves a tagging on a deleted tag. Of two merges of one tag, one wins and the other gets TagNotFoundException (see Concurrency and integrity).

A plain $tag->delete()

// An admin panel calling Eloquent directly — safe, but it detaches nothing
$tag->delete();

// Reads ignore the leftover taggings at once; a restore through the package starts clean,
// and tags:doctor reports them as an error until you run:
// php artisan tags:repair --orphans

The Tag model

RoundlyConsulting\Tags\Models\Tag is swappable through tags.models.tag (a subclass). It uses sluggable’s HasSlug, translatable’s HasTranslations, SoftDeletes and HasFactory:

$jazz->type;                   // 'genre'
$jazz->name;                   // in the current locale
$jazz->slug;                   // in the current locale
$jazz->slugFor('sk');          // one locale's slug, or null
$jazz->description;
$jazz->color;                  // '#f24e29' or null
$jazz->icon;
$jazz->parent_id;
$jazz->depth;                  // 1 for a root
$jazz->position;               // among its siblings
$jazz->usage_count;            // taggings, kept in the same transaction as each write
$jazz->definition();           // its TagType

Tag::query()->ofType('genre')->within($team)->roots()->ordered()->get();
AttributeMeaning
typeThe type name; never changes.
scope_type, scope_id, scope()The owner of a scoped tag (null for a global one); never changes. scope_type is hidden from serialisation.
scope_keyInternal and hidden: '' for a global tag, the owner’s morph class and key otherwise, so the unique slug index binds global tags too. Derived on every save.
parent_id, path, depthThe tree: path is the materialised chain of ancestor keys (/ for a root), depth 1 for a root.
positionThe order among siblings (0 first).
name, description, slugTranslatable: read in the current locale; getTranslations('name') gives the locale map, slugFor($locale) one locale’s slug.
color, icon#rrggbb or null; an identifier or an emoji, or null.
usage_countTaggings of the tag, adjusted in the same transaction as every write — every taggable class, soft-deleted taggables included.
wasRecentlyRestoredA plain property, the counterpart of wasRecentlyCreated: the instance was just brought back from deleted.
MethodDoes
definition()The tag’s resolved TagType.
parent() / children()BelongsTo and HasMany; children by position.
ancestors()Root first, in one query — or none, when loadAncestors() loaded them. A new collection either way.
descendants() / siblings()Builders of the live tags below it, and of the live tags with the same parent, type and scope.
isRoot()No parent.
taggings()HasMany of the pivot rows (Models\Tagging).
taggedModels($class)The models of one class carrying the tag — read-only, like tags().
ofType(), within($scope, $includeGlobal = true), global(), roots(), descendantsOf($tag, self: false), ordered()Query scopes on Tag::query().

Show your open-source love

This package is free and MIT-licensed. If it saves you time, a one-off donation or a Patreon membership keeps it maintained, tested and documented.

More ways to support, including crypto

By donating, you agree to our donation terms.

Want this built into your product?

We integrate our packages into custom Laravel and AI builds. Tell us what you're working on and we'll reply within 48 hours.