<?php

namespace App;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class MaterialCategory extends BaseModel
{
    use HasFactory;

    protected $fillable = [
		'id',
        'name',
        'slug',
        'parent_id',
        'unit',
        'is_activated',
        'is_deleted',
		'created_at',
		'updated_at'
    ];
	
	public function parent() {
		return $this->belongsTo(MaterialCategory::class, 'parent_id', 'id')
			->with('parent')
			->select('id', 'name', 'slug', 'parent_id', 'unit')
			->where('is_deleted', false)
			->where('is_activated', true)
			->orderBy('id', 'ASC');
	}
	
	public function children() {
		return $this->hasMany(MaterialCategory::class, 'parent_id', 'id')
			->with('children')
			->select('id', 'name', 'slug', 'parent_id', 'unit')
			->where('is_deleted', false)
			->where('is_activated', true)
			->orderBy('id', 'ASC');
	}
	
	public function item() {
		return $this->hasMany(MaterialCategory::class, 'parent_id', 'id')
			->select('id', 'name', 'slug', 'parent_id', 'unit')
			->where('is_deleted', false)
			->where('is_activated', true)
			->orderBy('id', 'ASC');
	}
}
