74 lines
1.2 KiB
PHP
74 lines
1.2 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Created by Reliese Model.
|
|
*/
|
|
|
|
namespace App\Models;
|
|
|
|
use Carbon\Carbon;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Kalnoy\Nestedset\NodeTrait;
|
|
use Illuminate\Support\Str;
|
|
|
|
|
|
|
|
/**
|
|
* Class Category
|
|
*
|
|
* @property int $id
|
|
* @property string $name
|
|
* @property string|null $slug
|
|
* @property bool $enabled
|
|
* @property Carbon|null $created_at
|
|
* @property Carbon|null $updated_at
|
|
* @property int $_lft
|
|
* @property int $_rgt
|
|
* @property int|null $parent_id
|
|
*
|
|
* @package App\Models
|
|
*/
|
|
class Category extends Model
|
|
{
|
|
use NodeTrait;
|
|
|
|
protected $table = 'categories';
|
|
|
|
protected $casts = [
|
|
'enabled' => 'bool',
|
|
'_lft' => 'int',
|
|
'_rgt' => 'int',
|
|
'parent_id' => 'int'
|
|
];
|
|
|
|
protected $fillable = [
|
|
'name',
|
|
'slug',
|
|
'enabled',
|
|
'_lft',
|
|
'_rgt',
|
|
'parent_id'
|
|
];
|
|
|
|
protected static function boot()
|
|
{
|
|
parent::boot();
|
|
|
|
static::saved(function ($category) {
|
|
if (empty($category->slug)) {
|
|
$category->slug = Str::slug($category->name);
|
|
$category->saveQuietly();
|
|
}
|
|
});
|
|
}
|
|
|
|
public function saveQuietly(array $options = [])
|
|
{
|
|
return static::withoutEvents(function () use ($options) {
|
|
return $this->save($options);
|
|
});
|
|
}
|
|
|
|
|
|
}
|