2018-05-31 03:00:20 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models\Observers;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a slug from a name
|
2020-03-29 01:03:52 +08:00
|
|
|
*
|
|
|
|
* @property object attributes
|
2018-05-31 03:00:20 +08:00
|
|
|
*/
|
|
|
|
class Sluggable
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @param $model
|
|
|
|
*/
|
|
|
|
public function creating($model): void
|
|
|
|
{
|
|
|
|
$model->slug = str_slug($model->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $model
|
|
|
|
*/
|
|
|
|
public function updating($model): void
|
|
|
|
{
|
|
|
|
$model->slug = str_slug($model->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $name
|
|
|
|
*/
|
|
|
|
public function setNameAttribute($name): void
|
|
|
|
{
|
|
|
|
$this->attributes['name'] = $name;
|
|
|
|
$this->attributes['slug'] = str_slug($name);
|
|
|
|
}
|
|
|
|
}
|