File: //home/armgroup/libs/app/Models/Category.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Spatie\Activitylog\LogOptions;
use Spatie\Activitylog\Traits\LogsActivity;
class Category extends Model
{
use HasFactory, LogsActivity;
protected $guarded = ['id'];
protected $casts = [
'faq' => 'array',
];
public const TYPE = [
[
'name' => 'بلاگ',
'slug' => 'article',
'className' => Article::class,
],
[
'name' => 'تماس با ما',
'slug' => 'contact',
'className' => Contact::class,
],
[
'name' => 'محصولات',
'slug' => 'product',
'className' => Product::class,
],
[
'name' => 'سوالات متداول',
'slug' => 'faq',
'className' => Faq::class,
],
];
public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults()
->logUnguarded()
->setDescriptionForEvent(fn (string $eventName) => "دستهبندیها ({$this->name}) ".eventNameTranslate($eventName));
}
public function getRouteKeyName()
{
return 'slug';
}
protected static function boot()
{
parent::boot();
static::addGlobalScope('order', function (Builder $builder) {
$builder->orderByRaw('ISNULL(sort_order), sort_order ASC');
});
}
public function children()
{
return $this->hasMany(Category::class, 'parent_id');
}
public function activeChildren()
{
return $this->children()->where('status', true);
}
public function parent()
{
return $this->belongsTo(Category::class, 'parent_id');
}
public function articles()
{
return $this->morphedByMany(Article::class, 'categorizable');
}
public function contacts()
{
return $this->morphedByMany(Contact::class, 'categorizable');
}
public function products()
{
return $this->morphedByMany(Product::class, 'categorizable')->withPivot('is_main');
}
public function getCategoryType()
{
return collect(self::TYPE)->firstWhere('className', $this->type);
}
public function scopeGetCategories($query, $type)
{
return $query->where('status', true)
->where('type', $type)
->select('id', 'name')
->get();
}
public function attributeGroups()
{
return $this->belongsToMany(AttributeGroup::class, 'attribute_group_category', 'category_id', 'group_id');
}
public function getParentsAttribute()
{
$parents = collect([]);
$parent = $this;
while (! is_null($parent)) {
$parents->push($parent);
$parent = $parent->parent;
}
return $parents->reverse();
}
public function getAllActiveChildren()
{
$sections = collect([]);
$this->load('activeChildren');
foreach ($this->activeChildren as $section) {
$sections->push($section);
$sections = $sections->merge($section->getAllActiveChildren());
}
return $sections;
}
public function getAllChildren()
{
$sections = collect([]);
$this->load('children');
foreach ($this->children as $section) {
$sections->push($section);
$sections = $sections->merge($section->getAllChildren());
}
return $sections;
}
public function productArticles()
{
return $this->belongsToMany(Article::class, 'article_category', 'category_id', 'article_id');
}
public function faqs()
{
return $this->morphedByMany(Faq::class, 'categorizable');
}
}