<?php

namespace Modules\Category\Entities;

use App\Models\BaseModel;
use Modules\Product\Entities\Model as Product;
use Modules\Slider\Entities\Model as Slider;

class Model extends BaseModel
{
    protected $guarded = [];

    protected $table = 'categories';

    protected $with = ['children'];

    public function Products()
    {
        return $this->belongsToMany(Product::class, 'product_categories', 'category_id', 'product_id')->orderBy('arrangement');
    }

    public function children()
    {
        return self::hasMany(Model::class, 'parent_id')->where('status', 1);
    }

    public static function boot()
    {
        parent::boot();
        static::created(function ($model) {
            $model->arrangement = $model->id;
            $model->save();
        });
    }

    public function sliders()
    {
        return $this->morphMany(Slider::class, 'linkable');
    }
}
