Download the PHP package oliwol/laravel-slugify without Composer

On this page you can find all versions of the php package oliwol/laravel-slugify. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-slugify

πŸŒ€ Laravel Slugify

Latest Version on Packagist GitHub Tests Action Status License

Documentation | Migrating from Spatie

A lightweight package that gives your Eloquent models clean, automatic slugs β€” without setup, ceremony, or extra weight.

Attach a PHP attribute or use the HasSlug trait, define the source (an attribute, multiple attributes, or a method), and the package quietly handles generation, updates and uniqueness.


πŸš€ Installation

Requirements: PHP 8.4+, Laravel 11+

Install the package via Composer:

⚑️ Quick Start

Using the PHP Attribute (recommended)

Using method overrides

Priority: Method overrides always take precedence over the #[Slugify] attribute.

Without any trait (attribute-only)

For simpler models that don't need findBySlug, slug history, or translatable slugs β€” you can skip the trait entirely:

Publish the config and register your models in the models array:

The service provider then automatically generates slugs for these models on every save event. findBySlug, slug history, and translatable slugs require HasSlug β€” use the trait when you need those features.

πŸ› οΈ Usage

Add the trait to any Eloquent model where a slug should be automatically generated.

Configuration via #[Slugify] Attribute

The #[Slugify] attribute accepts the following parameters:

Route model binding: Use routeBinding: true to automatically configure getRouteKeyName() without a manual override. Requires to: to be set.

Configuration via methods

Alternatively, you can configure slug generation by overriding methods:

Make sure your table contains the slug column:

If you use scoping, you probably don’t want a global unique index. Example: slugs must be unique per tenant:

βš™οΈ How it works

With HasSlug trait

The trait hooks into the Eloquent saving event per model class:

With attribute-only usage

The service provider registers a wildcard listener that fires on every Eloquent saving event. It skips models that already use HasSlug (to avoid double processing) and models not listed in config('slugify.models').

In both cases, the slug pipeline:

  1. Resolve the source β€” from the #[Slugify] attribute or a getAttributeToCreateSlugFrom() override. Supports a single attribute, multiple attributes, or a model method name.
  2. Generate a slug β€” if the source is a method, call it; otherwise combine filled attribute values (null/empty values are skipped).
  3. Skip regeneration if:

    1. Using attribute source(s) and none are dirty (unchanged), or
    2. The slug has been manually set and differs from the original.

    Note: When using a method source, dirty detection is skipped β€” the slug is always regenerated on save, since the package cannot track the method's dependencies.

  4. Ensure uniqueness by incrementing existing slugs (my-post, my-post-2, my-post-3, …).

πŸ”Ž Finding Models by Slug

The trait provides two static methods to look up models by their slug:

Both methods respect the configured slug column (to / getAttributeToSaveSlugTo()) and apply scopeSlugQuery() for scoped lookups.

πŸ“œ Slug History

When slugs change (e.g. because a title was updated), old URLs break. The optional HasSlugHistory trait keeps track of previous slugs so you can implement 301 redirects from old URLs to new ones β€” critical for SEO.

Setup

Publish and run the migration:

This creates a slug_history table that stores old slugs with a polymorphic relation to your models.

Usage

Add the HasSlugHistory trait alongside HasSlug:

When a slug changes, the old slug is automatically recorded in the slug_history table. Duplicate entries are prevented β€” if a model cycles back to a previous slug, it won't be stored again.

Finding models by current or historical slug

Use findBySlugWithHistory() to look up a model by its current slug or any previous slug:

Implementing 301 redirects

The easiest way is the slug.redirect middleware. Add it to any route that uses slug-based route model binding β€” it handles the redirect automatically:

The redirect status defaults to 301. Publish the config to change it:

Alternatively, handle the redirect manually in the controller:

Accessing slug history

You can access all previous slugs of a model via the slugHistory relationship:

🌍 Translatable Slugs

For multilingual applications, the optional HasTranslatableSlug trait integrates with spatie/laravel-translatable to generate one slug per locale (e.g. /en/hello-world and /de/hallo-welt).

Setup

Install spatie/laravel-translatable:

In your migration, define the source and slug columns as json:

Usage

Use HasTranslatableSlug instead of HasSlug and add spatie's HasTranslations trait:

Generating slugs per locale

When the model is saved, a slug is generated for each locale that has a value in the source attribute:

Per-locale uniqueness

Uniqueness is checked per locale using JSON-path queries. Two models can share the same English slug as long as their other locales differ β€” but within a single locale, suffixes are appended:

Finding models by translated slug

findBySlug() accepts an optional $locale parameter (defaults to app()->getLocale()):

Method sources

Method sources work too β€” the method is called once per locale with the locale context active:

Note: When using a method source with HasTranslatableSlug, the available locales are gathered from all translatable attributes on the model (excluding the slug target). For attribute sources, locales come from the source attribute itself.

Limitation: HasTranslatableSlug only supports a single attribute name or a method name as source. Array sources (e.g. from: ['first_name', 'last_name']) are not supported β€” use a method source instead to combine multiple translatable values.

πŸ“‘ Events

The package dispatches events during the slug lifecycle, allowing you to hook in for logging, cache invalidation, or redirect management.

Available events

Event Dispatched when Properties
Oliwol\Slugify\Events\SlugGenerated A slug is created for the first time $model, $slug
Oliwol\Slugify\Events\SlugUpdated An existing slug changes to a new value $model, $oldSlug, $newSlug

Events are only dispatched when the slug actually changes β€” if a save results in the same slug value, no event is fired.

Listening to events

Register listeners in your EventServiceProvider or use closures:

Combining with Slug History

When using both HasSlugHistory and events, the slug history is recorded automatically by the trait while events give you additional flexibility for custom logic. They work independently and can be used together or separately.

πŸ”§ Artisan Command

The package provides an Artisan command to generate or regenerate slugs for existing database records β€” useful when adding the package to an existing project or after changing slug configuration.

Generate missing slugs

This processes all records where the slug column is null or empty, generates a slug from the configured source attribute(s), and saves the result. Records that already have a slug are skipped.

The command works for both HasSlug models and attribute-only models registered in config/slugify.php.

Overwrite existing slugs

Use --force to regenerate slugs for all records, including those that already have one:

Preview changes

Use --dry-run to see how many slugs would be generated without actually saving anything:

Performance

The command processes records in chunks of 200 and displays a progress bar, making it safe to use on large datasets without running into memory issues.

βœ… Validation

The package provides two complementary validation rules:

Format: SlugFormat

Use SlugFormat to validate that a string is a proper slug β€” lowercase, alphanumeric, no leading/trailing/consecutive separators:

Uniqueness: SlugRule

Use SlugRule to validate that a slug doesn't already exist in the database β€” respecting the configured slug column and scoping automatically:

Both rules can be combined:

SlugRule uses the model's configured slug column (to / getAttributeToSaveSlugTo()) and applies scopeSlugQuery() automatically. Use ->ignore($model) for update scenarios and ->scope($column, $value) for additional constraints.

πŸ§ͺ Testing

When using model factories, slugs are generated automatically via the saving hook on create(). For make() β€” which does not persist the model β€” no slug is generated. Use the withSlug() macro to control slug generation explicitly in tests:

The macro is registered automatically via the service provider and works with any factory for a model that uses HasSlug or has a #[Slugify] attribute.

βœ… Best practices & caveats

πŸ” Custom Scoping Example

To ensure slugs are unique per tenant, override the scopeSlugQuery() method:

This will append a WHERE tenant_id = ? clause when checking for existing slugs.

πŸ“„ License

This package is open-sourced software licensed under the MIT license.


All versions of laravel-slugify with dependencies

PHP Build Version
Package Version
Requires php Version ^8.4|^8.5
illuminate/database Version ^11.0|^12.0|^13.0
illuminate/support Version ^11.0|^12.0|^13.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package oliwol/laravel-slugify contains the following files

Loading the files please wait ...