1 hour ago · Tech · 0 comments

The spatie/laravel-sluggable package has been around for close to a decade. A slug is the readable part of a URL that identifies a record, like announcing-laravel-sluggable-v4-with-self-healing-urls in this post's URL. The package generates one for any Eloquent model when you save it, derived from a title or another text field, and most of the time you don't have to think about it. We just released v4, which adds a few things worth talking about. Let me walk you through them. Generating slugs with an attribute For most models, slug generation is mechanical. Pick a source field, pick a destination field, done. In v4, you can express that with an attribute on the model. use Spatie\Sluggable\Attributes\Sluggable; use Illuminate\Database\Eloquent\Model; #[Sluggable(from: 'title', to: 'slug')] class Post extends Model { } That's it. No trait, no getSlugOptions() method. Save a Post and the package fills in the slug: $post = Post::create(['title' => 'ActiveRecord is awesome']); $post->slug;…

No comments yet. Log in to reply on the Fediverse. Comments will appear here.