kodooy / laravel-slug-generator
Laravel package for automatic slug generation using traits
v1.0.1
2025-07-31 22:23 UTC
Requires
- php: ^8.1
- illuminate/database: ^9.0|^10.0|^11.0|^12.0
- illuminate/support: ^9.0|^10.0|^11.0|^12.0
Requires (Dev)
- orchestra/testbench: ^7.0|^8.0|^9.0|^10.0
- phpunit/phpunit: ^9.0|^10.0|^11.0
README
A Laravel package that provides automatic slug generation for Eloquent models using traits.
Installation
composer require kodooy/laravel-slug-generator
Database Setup
Your Eloquent model's database table must have a slug
field. Add it to your migration:
Schema::create('posts', function (Blueprint $table) { $table->id(); $table->string('name'); $table->string('slug')->unique()->index(); $table->timestamps(); });
If adding to an existing table:
Schema::table('posts', function (Blueprint $table) { $table->string('slug')->unique()->index(); });
Usage
Add the Slugable
trait to your Eloquent model:
use Kodooy\SlugGenerator\Traits\Slugable; class Post extends Model { use Slugable; // The trait will automatically generate slugs from the 'name' attribute // and use 'slug' as the route key }
Customization
Override these methods to customize behavior:
class Post extends Model { use Slugable; // Use different attribute for slug generation protected function slugableAttribute() { return 'title'; // default is 'name' } // Preserve existing slugs on update protected function preserveSlugOnUpdate() { return true; // default is false } }
Automatic Slug Generation
// Slug is automatically generated when creating/updating $post = Post::create([ 'title' => 'My Blog Post', 'content' => 'This is the content...' ]); // Slug will be: 'my-blog-post' echo $post->slug;
Manual Slug Generation
// Generate slug manually $post = new Post(['title' => 'Another Great Post']); $post->slug = $post->generateSlug();
Unique Slug Handling
// If a slug already exists, it will be made unique: // "my-post" -> "my-post-1" -> "my-post-2" etc. $post1 = Post::create(['title' => 'My Post']); // slug: "my-post" $post2 = Post::create(['title' => 'My Post']); // slug: "my-post-1" $post3 = Post::create(['title' => 'My Post']); // slug: "my-post-2"
Requirements
- PHP ^8.1
- Laravel ^9.0|^10.0|^11.0|^12.0
License
MIT