Laravel Eloquent Sluggable is a package that simplifies the creation and management of slugs for Eloquent models in Laravel applications. A slug is a URL-friendly version of a string, often used in URLs to identify resources in a readable way.
Key Features of Laravel Eloquent Sluggable:
- Automatic Slug Generation: Automatically generates slugs from a specified field in your model (e.g., titles).
- Customizable Slug Format: Customize the format of the slug to fit your needs.
- Unique Slugs: Ensure that generated slugs are unique, avoiding conflicts with existing entries.
- Slug Updates: Automatically update slugs when the original field is modified.
- Integration with Eloquent: Seamlessly integrates with Laravel’s Eloquent ORM.
Installation
To get started with Laravel Eloquent Sluggable, follow these steps:
- Require the Package: Install the package via Composer:
1composer require spatie/eloquent-sluggable - Publish the Configuration (optional): You can publish the configuration file using:
1php artisan vendor:publish --provider="Spatie\EloquentSluggable\ServiceProvider" - Setup the Model: You need to set up your model to use the Sluggable trait. For example, if you have a
Post
model:
123456789101112131415161718192021namespace App\Models;use Illuminate\Database\Eloquent\Model;use Spatie\Sluggable\Sluggable;use Spatie\Sluggable\SlugOptions;class Post extends Model{use Sluggable;protected $fillable = ['title', 'content'];public function getSlugOptions() : SlugOptions{return SlugOptions::create()->generateSlugsFrom('title') // Field to generate slug from->saveSlugsTo('slug') // Field to save the slug->usingLanguage('en') // Set language for slug->slugsShouldBeUnique(); // Ensure slugs are unique}}
Database Migration
Ensure your database migration includes a slug
column in your model’s table. For example, for the posts
table:
1 2 3 4 5 6 7 |
Schema::create('posts', function (Blueprint $table) { $table->id(); $table->string('title'); $table->string('slug')->unique(); $table->text('content'); $table->timestamps(); }); |
Using the Model
Now that the model is set up, you can create a new post, and a slug will be generated automatically based on the title.
- Creating a Post: When you create a new post, the slug will be generated from the title:
123456$post = Post::create(['title' => 'My First Post','content' => 'This is the content of my first post.',]);// The slug will be automatically generated and saved. - Updating the Post: If you update the title, the slug will automatically update if you configure it to do so:
1234$post->title = 'My Updated First Post';$post->save();// The slug will be updated automatically.
Custom Slug Format
You can customize the slug format by chaining additional methods in the getSlugOptions
method. For example, if you want to add a timestamp to the slug:
1 2 3 4 5 6 7 8 9 10 11 |
public function getSlugOptions() : SlugOptions { return SlugOptions::create() ->generateSlugsFrom('title') ->saveSlugsTo('slug') ->usingLanguage('en') ->slugsShouldBeUnique() ->appendSlug('{slug}-{timestamp}', function () { return now()->timestamp; }); } |
Conclusion
Laravel Eloquent Sluggable makes managing slugs in your Laravel applications straightforward. By automating the generation and management of slugs, it helps you maintain clean, user-friendly URLs.
Additional Considerations
- Documentation: For more detailed information, advanced features, and configuration options, refer to the official Spatie Eloquent Sluggable documentation.
- Handling Duplicates: The package can automatically handle slug conflicts by appending a unique suffix (e.g.,
my-post-1
,my-post-2
) if configured.