38 lines
748 B
PHP
38 lines
748 B
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
class Category extends Model
|
|
{
|
|
protected $fillable = [
|
|
'name',
|
|
'slug',
|
|
'type',
|
|
'parent_id',
|
|
'sort',
|
|
];
|
|
|
|
protected $casts = [
|
|
'parent_id' => 'integer',
|
|
'sort' => 'integer',
|
|
];
|
|
|
|
public function products(): HasMany
|
|
{
|
|
return $this->hasMany(Product::class, 'category_id');
|
|
}
|
|
|
|
public function articles(): HasMany
|
|
{
|
|
return $this->hasMany(Article::class, 'category_id');
|
|
}
|
|
|
|
public function scopeOfType($query, string $type)
|
|
{
|
|
return $query->where('type', $type)->orderBy('sort')->orderBy('name');
|
|
}
|
|
}
|