This snippet code automatically generate a url-friendly slug where spaces are converted into dash character for website address, normally use in some blog page of the website
class Item extends ModelSlug is a Laravel helper that converts a given string into a dash separated string
{
public static function boot()
{
parent::boot();
static::saving(function ($model) {
$model->slug = str_slug($model->name);
});
}
}
str_slug()
The str_slug
function generates a URL friendly "slug" from the given string:
$title = str_slug('Laravel 5 Framework', '-');
Author :
Dario Mindoro
Author of Mindworksoft.com, Full-stack developer, interested in media streaming, automation, photography, AI, and digital electronics