Current File : /home/tradevaly/text.tradevaly.com.bd/app/Models/Category.php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use Illuminate\Database\Eloquent\SoftDeletes;

class Category extends Model
{
    use HasFactory,SoftDeletes;
    
    public function parent(){
        return $this->belongsTo(Category::class, 'parent_id', 'id');
    }
    public function sub(){
        return $this->belongsTo(Category::class, 'sub_id', 'id')->where('sub_id', '!=', 0);
    }
    public function categories(){
        return $this->hasMany(Category::class, 'id','parent_id');
    }
    
    protected static function boot() {
        parent::boot();

        static::creating(function ($question) {
            $question->slug = Str::slug($question->name);
        });
    }
}