The Sluggable trait generates unique and SEO friendly slug for model instance when you create or edit the model. It uses the name attribute by default, but can be customized to use any attribute on the model.
To use the Sluggable trait, add it to your model:
use Teners\LaravelExtras\Traits\Sluggable;
class Post extends Model
{
use Sluggable;
}
By default, the trait uses the name
attribute of the model to generate the slug. You can customize this by setting the slugSourceColumn
property on the model:
class Post extends Model
{
use Sluggable;
protected string $slugSourceColumn = 'title';
}
This will use the name attribute of the model to generate the slug.
Add a slug column to your migratiion:
...
$table->string('name');
$table->string('slug')->unique(); // Here
...
Use any of Laravel's methods to create or edit the model instance without specifying the slug.
Example:
// Create
Post::create([
'name' => 'Technology in Africa',
]);
// Save
$post = Post::find(1);
$post->update([
'name' => 'Technologies in Africa',
]);
or
$post->name = 'Technologies in Africa';
$post->save();