Product::whereSlug('red-chair')->first(); // default column
Product::whereSlug('rc-01', column: 'handle')->first();
Topic::whereSlug('investovanie', locale: 'sk')->first(); // exact locale
Topic::whereSlug('investing')->first(); // chain: current → fallback → supported
Topic::whereSlugIn(['a', 'b'])->get();
Topic::whereSlugInAnyLocale('investing')->first(); // current, fallback, supported, listed
Topic::whereSlug('gift')->orderBySlugPreference('gift')->first();
Product::findBySlug('red-chair'); // ?Product (adds the preference order)
Product::findBySlugOrFail('red-chair'); // ModelNotFoundException → 404- whereSlug($slug, column:, locale:) — an explicit locale is exact; null follows the definition’s chain: current, then fallback, then (for Any) every supported locale and any listed in locales().
- whereSlugIn() takes a list, whereSlugInAnyLocale() ignores the chain and searches every resolvable locale (current, fallback, supported, listed), orderBySlugPreference() ranks the current locale first, then the fallback.
- findBySlug() returns the model or null and adds the preference order; findBySlugOrFail() throws ModelNotFoundException — a 404 in a controller.
- Passing a locale for a string column throws InvalidSlugDefinitionException (localeOptionOnStringColumn).
Scopes add predicates only — never an ORDER BY — so ->count(), ->paginate() and whereHas() sub-queries work on every engine; only the finders and route binding add the locale-preference order. Reads apply the model’s normal global scopes.
Packages with a swappable model should call the scopes through their seam (PostModel::query()->whereSlug(…)) rather than Post::findBySlug(…) — late static binding ignores the swap.
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 cryptoBy 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.