Generating your first slug | laravel-sluggable-rust | Spatie       

 SPATIE  

  Laravel Sluggable 
====================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-sluggable-rust](https://spatie.be/docs/laravel-sluggable-rust/v1)  Basic-usage  Generating your first slug

 Version   v1      

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

  Generating your first slug    
- [ Introduction ](https://spatie.be/docs/laravel-sluggable-rust/v1/introduction)
- [ Requirements ](https://spatie.be/docs/laravel-sluggable-rust/v1/requirements)
- [ Support us ](https://spatie.be/docs/laravel-sluggable-rust/v1/support-us)
- [ Installation and setup ](https://spatie.be/docs/laravel-sluggable-rust/v1/installation-setup)
- [ Translatable slugs ](https://spatie.be/docs/laravel-sluggable-rust/v1/translatable-slugs)
- [ Changelog ](https://spatie.be/docs/laravel-sluggable-rust/v1/changelog)
- [ Questions and issues ](https://spatie.be/docs/laravel-sluggable-rust/v1/questions-issues)
- [ Upgrading ](https://spatie.be/docs/laravel-sluggable-rust/v1/upgrading)
- [ Testing ](https://spatie.be/docs/laravel-sluggable-rust/v1/testing)
- [ Releasing ](https://spatie.be/docs/laravel-sluggable-rust/v1/releasing)

Basic usage
-----------

- [ Generating your first slug ](https://spatie.be/docs/laravel-sluggable-rust/v1/basic-usage/getting-started)
- [ Using slug options ](https://spatie.be/docs/laravel-sluggable-rust/v1/basic-usage/using-slug-options)
- [ Finding models by slug ](https://spatie.be/docs/laravel-sluggable-rust/v1/basic-usage/finding-models-by-slug)
- [ Self-healing URLs ](https://spatie.be/docs/laravel-sluggable-rust/v1/basic-usage/self-healing-urls)

Advanced usage
--------------

- [ Combining multiple source columns ](https://spatie.be/docs/laravel-sluggable-rust/v1/advanced-usage/source-fields)
- [ Tuning the uniqueness suffix ](https://spatie.be/docs/laravel-sluggable-rust/v1/advanced-usage/uniqueness)
- [ Overriding the underlying actions ](https://spatie.be/docs/laravel-sluggable-rust/v1/advanced-usage/overriding-actions)

 Generating your first slug
==========================

###  On this page 

1. [ 1. Configure the model ](#content-1-configure-the-model)
2. [ 2. Add a slug column to the migration ](#content-2-add-a-slug-column-to-the-migration)
3. [ 3. Create a record ](#content-3-create-a-record)
4. [ 4. Use the slug in a URL ](#content-4-use-the-slug-in-a-url)

This walkthrough takes a `Post` model from nothing to a working slug in three steps. An optional fourth step wires the slug into a route with self-healing URLs so renaming a model never breaks an old link.

1. Configure the model
------------------------------------------------------------------------------------------------------------------------

Tell the package which column to read and which to write. The setter assigns the generated value when an existing model is updated; creation writes directly into Laravel's draft.

```
use laravel::model::model;
use spatie_sluggable::{SlugOptions, slug_hooks};

#[model(hooks)]
pub struct Post {
    pub id: i64,
    pub title: String,
    pub slug: Option<String>,
}

impl Post {
    fn slug_options() -> SlugOptions<Self, Option<String>> {
        SlugOptions::create(Self::slug, |post, slug| post.slug = slug)
            .generate_slugs_from(Self::title)
    }
}

slug_hooks!(Post, Post::slug_options());
```

The macro implements Laravel's `Hooks` trait. It is optional. If the model already has hooks, call `Post::slug_options().on_create(draft).await` in `creating` and `.on_update(self, changes).await` in `updating`.

2. Add a slug column to the migration
---------------------------------------------------------------------------------------------------------------------------------------------------------------------

The package writes the slug into the column you configured, so the table needs a matching string column. In your table migration:

```
tx.schema().create("posts", |table| {
    table.id();
    table.string("title");
    table.string("slug").nullable().unique();
}).await?;
```

3. Create a record
------------------------------------------------------------------------------------------------------------

```
let post = create!(Post { title: "Hello World" }).await?;
assert_eq!(post.slug.as_deref(), Some("hello-world"));

let another = create!(Post { title: "Hello World" }).await?;
assert_eq!(another.slug.as_deref(), Some("hello-world-1"));
```

Slugs regenerate on model updates. Use `do_not_generate_slugs_on_update()` for permanent slugs or `prevent_overwrite()` to retain an existing value.

4. Use the slug in a URL
------------------------------------------------------------------------------------------------------------------------------

For immutable slugs, use Laravel's ordinary `{post:slug}` binding or set `#[model(hooks, route_key = "slug")]`.

If slugs may change, use [self-healing URLs](self-healing-urls). Resolve the composite key explicitly in the handler, passing the existing scoped query so tenant and relationship constraints stay in place.

 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)
