---
title: "Schema & unique indexes — Sluggable for Laravel | Roundly"
description: "Blueprint macros for slug columns and engine-native unique indexes for string and locale-map slugs on PostgreSQL, MySQL and SQLite."
url: https://roundly-consulting.com/open-source/docs/sluggable-for-laravel/indexes
language: en
---

[All packages](https://roundly-consulting.com/open-source.md)

[Sluggable for Laravel](https://roundly-consulting.com/open-source/docs/sluggable-for-laravel.md)

# Schema & unique indexes

Three Blueprint macros cover the columns and the simple string index:

```php
$table->slug('slug');                                // string(255)
$table->localizedSlug('slug');                       // jsonb (pgsql) / json (mysql) / text (sqlite)
$table->uniqueSlug('slug', scope: ['clinic_id']);    // string slug, trashed rows count as taken
```

Locale maps, and string slugs that exclude trashed rows, need engine-specific DDL. SlugIndexes builds it — run it after Schema::create():

```php
use RoundlyConsulting\Sluggable\DataTransferObjects\SlugIndexSpec;
use RoundlyConsulting\Sluggable\Schema\SlugIndexes;

// after Schema::create(…):
SlugIndexes::ensure(SlugIndexSpec::localeMap('topics', 'slug', scope: ['shop_id']));
SlugIndexes::ensure(SlugIndexSpec::string('articles', 'slug', includeTrashed: false));
SlugIndexes::forModel(Topic::class);       // specs derived from the definitions (= Slugs::model(Topic::class)->indexes())
SlugIndexes::plan($spec);                  // DDL only
```

## In a migration

```php
use App\Models\Topic;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use RoundlyConsulting\Sluggable\Schema\SlugIndexes;

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('topics', function (Blueprint $table) {
            $table->id();
            $table->json('name');
            $table->localizedSlug('slug');
            $table->timestamps();
        });

        SlugIndexes::forModel(Topic::class);   // one unique index per supported locale
    }
};
```

ensure() and forModel() accept a dryRun flag and return a SlugIndexReport listing the created and skipped indexes plus the statements. Outside a migration, Slugs::model(Topic::class)->indexes() runs the same forModel() call.

## Per engine

| Engine | Locale map | Exclude-trashed string |
| --- | --- | --- |
| PostgreSQL | CREATE UNIQUE INDEX … (("slug"->>'en')) per locale, partial WHERE deleted\_at IS NULL when needed | Partial unique index |
| SQLite | CREATE UNIQUE INDEX … (json\_extract("slug", '$."en"')), partial when needed | Partial unique index |
| MySQL / MariaDB | An INVISIBLE virtual column slug\_\_en (varchar sized to maxLength, at least 255; utf8mb4\_bin; NULL for missing / JSON null / trashed) + a unique index | INVISIBLE slug\_\_live with the source collation + a unique index |

- Indexes are idempotent and named {table}\_{column}\[\_{locale}\]\_slug\_unique; names over 60 characters are shortened with a crc32 hash.
- Locales come from SlugLocales::supported() at migrate time; add a locale later with php artisan sluggable:indexes. Removed locales keep their index — drop it by hand.
- includeTrashed on a spec defaults to true — mirror the definition, or use forModel(), which also skips non-unique, closure-scoped and across-locales definitions.
- MySQL: locale-map indexes are binary (Café and cafe are different slugs, like the app-level check); string slug columns keep your collation. MySQL lookups are not accelerated by the generated columns — they enforce uniqueness only.
- MySQL: each locale column is sized varchar(maxLength), never below 255 — SlugIndexSpec::localeMap() takes maxLength: and forModel() fills it from the definition. Above 768 characters (the 3072-byte InnoDB key limit in utf8mb4) planning throws InvalidSlugDefinitionException (invalidOption), so lower maxLength for locale-map slugs on MySQL. An already-created column is not resized.
- SQL Server is not supported (MySQL and PostgreSQL are, SQLite for tests); its index DDL throws UnsupportedDriverException.

## 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](https://roundly-consulting.com/support-us.md)

By donating, you agree to our [donation terms](https://roundly-consulting.com/donation-terms.md).

[Support our open source work (opens in a new tab)](https://donate.stripe.com/dRmeVe8FX5PF1Qd9pXcEw00) [Join us on Patreon (opens in a new tab)](https://www.patreon.com/cw/roundly)

## 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.

[Get a quote in 48 hours](https://roundly-consulting.com/contact.md) [Browse all packages](https://roundly-consulting.com/open-source/docs/sluggable-for-laravel.md)
