So, Laravel News just highlighted the new Laravel Sluggable package for Eloquent models. It's an opinionated, automatic slug generation solution, and honestly, I think it's about damn time we got something this straightforward built into the ecosystem.
Why this matters for web developers
If you're building any kind of content-driven Laravel app, you know the drill: you need clean, readable URLs. That means transforming a post title like "My Awesome Blog Post With Special Characters!" into "my-awesome-blog-post-with-special-characters". This isn't just about aesthetics; good slugs are crucial for SEO, making your content more discoverable for search engines. But hand-rolling slug generation every time, dealing with uniqueness, and updating them when titles change? That's a repetitive chore. This package takes that entire headache away, letting you focus on the actual content and features, not string manipulation. We're talking about saving hours across a project with just a few models.
The technical reality
Setting this up is pretty painless. You pull in the package, add a trait to your Eloquent model, and tell it which field to use for the slug source. It's smart enough to handle uniqueness by default, appending numbers if needed. Let's say you have a Post model and want to slugify its title field. Here's how you'd get it running:
// In your Post model (app/Models/Post.php)
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Spatie\Sluggable\HasSlug;
use Spatie\Sluggable\SlugOptions;
class Post extends Model
{
use HasSlug;
protected $fillable = ['title', 'content', 'slug'];
public function getSlugOptions() : SlugOptions
{
return SlugOptions::create()
->generateSlugsFrom('title')
->saveSlugsTo('slug');
}
}
And you'll need to add a slug column to your database table. A simple migration handles that:
// In your migration file (e.g., 2023_10_27_create_posts_table.php)
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up() : void
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('content');
$table->string('slug')->unique(); // The magic happens here
$table->timestamps();
});
}
public function down() : void
{
Schema::dropIfExists('posts');
}
};
Now, whenever you create or update a post, the slug field gets populated automatically. It's that easy to add a core feature.
What I'd actually do today
- Install the package:
composer require spatie/laravel-sluggable. Spatie packages are usually solid, so I trust this one out of the box. - Add a
slugcolumn: Create a migration to add astringcolumn namedslugto my relevant Eloquent models, making sure it'sunique(). - Implement
HasSlugtrait: Dropuse HasSlug;and thegetSlugOptions()method into each model that needs slugs. - Configure
SlugOptions: SpecifygenerateSlugsFrom()to point to the correct source attribute (liketitleorname) andsaveSlugsTo()to the newslugcolumn. - Test it: Create a few new records, update some existing ones, and verify the slugs are generated correctly and are unique.
Gotchas & unknowns
While the package is great, there are always things to watch for. If you've got existing data without slugs, this package won't magically backfill them; you'll need a separate script or a php artisan tinker session to regenerate slugs for old records. Also, if your title field changes, the slug will regenerate by default. That's usually what you want, but if you need permanent, unchanging slugs even after a title edit, you'll need to configure it with doNotGenerateSlugsOnUpdate(). And while it handles uniqueness, complex edge cases with very similar titles might still produce less than ideal slugs, like my-post-1, my-post-2, my-post-3. It's a common issue, not unique to this package. Plus, this specific version I'm looking at doesn't handle multilingual slugs out of the box, which can be a real pain for global applications.
What's your preferred approach for managing slugs in your Laravel projects? Are you rolling your own, or does a package like this make more sense for your workflow?

Top comments (0)