39 lines
796 B
PHP
39 lines
796 B
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Str;
|
|
|
|
class Category extends Model
|
|
{
|
|
protected $fillable = [
|
|
'name',
|
|
'slug',
|
|
'description',
|
|
'image'
|
|
];
|
|
|
|
protected static function boot()
|
|
{
|
|
parent::boot();
|
|
|
|
static::creating(function ($category) {
|
|
if (empty($category->slug)) {
|
|
$category->slug = Str::slug($category->name);
|
|
}
|
|
});
|
|
|
|
static::updating(function ($category) {
|
|
if ($category->isDirty('name') && !$category->isDirty('slug')) {
|
|
$category->slug = Str::slug($category->name);
|
|
}
|
|
});
|
|
}
|
|
|
|
public function products()
|
|
{
|
|
return $this->hasMany(Product::class);
|
|
}
|
|
}
|