<?php namespace App\Models; use App\User; use Packages\Product\Models\Product; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; use Cviebrock\EloquentSluggable\Sluggable; class Category extends Model { protected $fillable = ['title', 'parent_id', 'category_type', 'creator_id', 'slug', 'discription']; use Sluggable; use SoftDeletes; public function user() { return $this->belongsTo(User::class, 'creator_id', 'id'); } public function children(){ return $this->hasMany( Category::class, 'parent_id' ); } public function parent() { return $this->belongsTo(Category::class); } public function products() { return $this->morphedByMany(Product::class, 'categorizable'); } public function getCategoryTypeAttribute($value) { switch ($value) { case 0: $category_type = 'محصولات'; break; case 1: $category_type = 'اخبار'; break; case 2: $category_type = 'مطالب'; break; default: $category_type = ''; } return $category_type; } public function sluggable() { return [ 'slug' => [ 'source' => 'title' ] ]; } }