Laravel Cloud is here! Zero-config managed infrastructure for Laravel apps. Deploy now.

Eloquent Sluggable

cviebrock/eloquent-sluggable image

Eloquent Sluggable stats

Downloads
8.2M
Stars
3,738
Open Issues
3
Forks
448

View on GitHub →

Easy creation of slugs for your Eloquent models in Laravel

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.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Cviebrock Eloquent Sluggable Related Articles

Export from Sequel Pro to a Laravel Migration image

Export from Sequel Pro to a Laravel Migration

Read article
PhpStorm logo

PhpStorm

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

PhpStorm
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Typesense Search logo

Typesense Search

Typesense is an open source, blazing-fast search engine, optimized for helping you build delightful search experiences for your sites and apps. Natively integrated with Laravel Scout.

Typesense Search
Tighten logo

Tighten

We help companies turn great ideas into amazing apps, products, and services.

Tighten
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
Curotec logo

Curotec

World class Laravel experts with GenAI dev skills. LATAM-based, embedded engineers that ship fast, communicate clearly, and elevate your product. No bloat, no BS.

Curotec