Package

Cviebrock Eloquent Sluggable

Easy creation of slugs for your Eloquent models in Laravel

Package

cviebrock/eloquent-sluggable

View on GitHub →

Eloquent-Sluggable Package Summary

Eloquent-Sluggable is a Laravel package designed to easily handle the creation and management of slugs for Eloquent models. It automates slug generation, ensuring they are SEO-friendly and unique, with minimal setup required.

Key Features

  • Automatic Slug Generation: Automatically generates slugs when models are saved.
  • Customizable Slugging Strategy: Configure slug generation strategies per model.
  • Unique Slugs: Ensures slugs are unique and provides options to customize the uniqueness constraint.
  • Support for Multiple Slugs: Ability to handle multiple slugs per model based on different attributes.
  • Event Hooks: Offers slugging and slugged model events for intercepting slug generation lifecycle.
  • Route Model Binding: Supports explicit route model binding using slugs.

Installation

Installation varies with Laravel versions, so ensure you install the correct package version matching your Laravel installation:

composer require cviebrock/eloquent-sluggable

To customize configurations, publish the config file:

php artisan vendor:publish --provider="Cviebrock\EloquentSluggable\ServiceProvider"

Configuration and Usage

Implement the Sluggable trait in your model and define the sluggable method to specify the source field and other options:

use Cviebrock\EloquentSluggable\Sluggable;

class Post extends Model
{
    use Sluggable;

    public function sluggable(): array
    {
        return [
            'slug' => [
                'source' => 'title'
            ]
        ];
    }
}

The slug is then automatically generated and saved when the model is created or updated. Access it simply via $model->slug.

Advanced Features

  • Custom Slug Generation: Override default settings per model for source fields and slug generation methods.
  • Handling Updates: Optionally regenerate slugs on model updates.
  • Event System: Utilize slugging and slugged events to add custom logic during the slug lifecycle.
  • Scopes and Helpers: Use scope helpers for slug-based queries and leverage custom query scopes to refine slug generation rules.

For developers looking to streamline their application URLs and improve SEO, Eloquent-Sluggable provides a robust, flexible solution for managing slugs in Laravel applications. The package simplifies the complexity behind slug generation and ensures that slugs are both readable and unique, enhancing the overall user experience.

Cviebrock Eloquent Sluggable Related Articles

Eloquent Sluggable Package image

Eloquent Sluggable Package

Read article
Laravel Sluggable image

Laravel Sluggable

Read article
Eloquent Sluggable image

Eloquent Sluggable

Read article
75 Laravel Tutorials, Packages, and Resources from 2014 image

75 Laravel Tutorials, Packages, and Resources from 2014

Read article
Export from Sequel Pro to a Laravel Migration image

Export from Sequel Pro to a Laravel Migration

Read article
Boost your Eloquent Models with Laravel Lift image

Boost your Eloquent Models with Laravel Lift

Read article

Let your users buy domains without leaving your app

If your Laravel app gives users a site, a storefront, or a workspace, they’ll eventually want their own domain. The name.com API lets them search, register, and connect one without leaving your product. It’s a clean REST API, so Laravel’s HTTP client is all you need.

Let your users buy domains without leaving your app

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing