Usage | laravel-tags-rust | Spatie       

 SPATIE  

laravel-tags-rust
=================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-tags-rust](https://spatie.be/docs/laravel-tags-rust/v1)  Usage

 Version   v1      

 Other versions for crawler [v1](https://spatie.be/docs/laravel-tags-rust/v1) 

  Usage    
- [ Introduction ](https://spatie.be/docs/laravel-tags-rust/v1/introduction)
- [ Support us ](https://spatie.be/docs/laravel-tags-rust/v1/support-us)
- [ Installation ](https://spatie.be/docs/laravel-tags-rust/v1/installation-and-setup)
- [ Usage ](https://spatie.be/docs/laravel-tags-rust/v1/usage)
- [ Custom models ](https://spatie.be/docs/laravel-tags-rust/v1/custom-models)
- [ Database contracts ](https://spatie.be/docs/laravel-tags-rust/v1/database)
- [ Changelog ](https://spatie.be/docs/laravel-tags-rust/v1/changelog)
- [ Testing ](https://spatie.be/docs/laravel-tags-rust/v1/testing)

 Usage
=====

###  On this page 

1. [ Names, slugs and translations ](#content-names-slugs-and-translations)
2. [ Assign and filter ](#content-assign-and-filter)
3. [ Order and deletion ](#content-order-and-deletion)

Names, slugs and translations
---------------------------------------------------------------------------------------------------------------------------------------------

Each context has an explicit locale. Lookups match either the name or slug, without translation fallback, in an exact namespace. `None` selects untyped tags, not all namespaces.

```
use spatie_tags::Tags;
use serde_json::json;

let en = Tags::new("en")?;
let mut tag = en.create(json!({"en":"Rust tips", "nl":"Rust advies"}), None).await?;
assert_eq!(tag.slug.0["en"], "rust-tips");
assert_eq!(en.find_or_create("rust-tips", None).await?.id, tag.id);

Tags::new("nl")?.rename(&mut tag, "Nieuwe tips").await?;
assert_eq!(tag.slug.0["nl"], "nieuwe-tips");
```

- `find` returns `Option<Tag>` and never creates rows.
- `matching` and `matching_many` return composable framework builders.
- `matching_any_type` searches every namespace.
- `find_many_or_create(&names, type)` resolves a batch with one initial lookup and preserves input order, including repeats, without creating duplicate tags.
- `containing` uses case-insensitive SQL LIKE; `%` and `_` remain wildcards.
- `Tags::<Tag>::types()` lists non-null namespaces.

`Tag` implements the re-exported `spatie_translatable::HasTranslations` trait. Its JSON fields can be read with that package's locale/fallback settings. For direct translation writes, edit a clone and pass the clone's name column to the original model's typed `update()` builder. The update hook regenerates slugs when names change. Bulk updates and raw SQL bypass hooks.

Assign and filter
-----------------------------------------------------------------------------------------------------------

```
use spatie_tags::{HasTags, TagQuery, Tags};

let tags = Tags::new("en")?;
let rust = tags.find_or_create("rust", None).await?;
post.taggable().attach(&[rust.id]).await?;
post.taggable().sync_names(&tags, &["rust", "web development"], None).await?;

let posts = Post::query()
    .with_all_tag_names(&tags, &["rust", "web-development"], None)
    .order_by("id")
    .get().await?;
assert_eq!(posts.len(), 1);
```

- `attach(ids)` keeps existing assignments; duplicate IDs are harmless.
- `sync(ids)` replaces the complete list, including typed tags. Empty input clears it.
- `sync_with_type(ids, type)` changes only that exact namespace and rejects mismatched IDs.
- `sync_names(context, names, type)` resolves names or slugs, deduplicates, and replaces the complete list in one transaction. Trim, lowercase or limit AI input in your application before calling it.
- `attach_names(context, names, type)` resolves names or slugs and keeps existing assignments.
- `sync_names_with_type(context, names, type)` resolves names or slugs and replaces only that namespace. Empty input clears only that namespace.
- `detach_names(context, names, type)` detaches matching names or slugs without creating unknown tags.
- `detach(ids)` and `detach_all()` remove assignments, never tag rows.
- `has_tag(id)` and `has_name(context, name, type)` check saved assignments.
- `tags()` and `tags_with_type(type)` return ordered native builders for further filters and pagination.
- `translated(context)` loads ordered models with `name_translated` and `slug_translated` beside the original JSON columns. Missing locales give `None`, without fallback.
- Filter IDs with `with_any_tags`, `with_all_tags` or `without_tags`. IDs may span namespaces.
- Filter an exact namespace with `with_any_tag_names`, `with_all_tag_names` or `without_tag_names`.
- Filter namespaces with `with_any_tag_types`.
- Search across namespaces with `with_any_tag_names_of_any_type` or `with_all_tag_names_of_any_type`. The all-of version requires every matching tag, including different namespaces sharing the same name.
- Name filters run entirely in SQL and do not create missing tags. An unknown required name matches no owners.
- Any-of empty input matches nothing.
- All-of empty input leaves the query unchanged.
- Without empty input excludes nothing.
- A missing required name makes an all-of filter match nothing.
- Filters return ordinary builders and compose with publication status, archived flags, counts and pagination without duplicating model rows.

Order and deletion
--------------------------------------------------------------------------------------------------------------

`Tags::<Tag>::set_order(&ids)` assigns positions starting at one. Duplicate or missing IDs reject the operation. Unlisted tags retain their positions; tied positions use ID order. New tags append after the greatest existing position. Concurrent creators can receive the same position.

Deleting a tag cascades its assignments through the foreign key. Register `spatie_tags::listen::<Owner>(events)` in `with_events` to detach assignments after a native owner deletion. Wrap owner deletion in a transaction if the delete and listener must roll back together. A bulk owner delete bypasses model events, so clean up its pivots explicitly.

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
