Category.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace App\Models;
  3. use App\User;
  4. use Packages\Product\Models\Product;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Database\Eloquent\SoftDeletes;
  7. use Cviebrock\EloquentSluggable\Sluggable;
  8. class Category extends Model
  9. {
  10. protected $fillable = ['title', 'parent_id', 'category_type', 'creator_id', 'slug', 'discription'];
  11. use Sluggable;
  12. use SoftDeletes;
  13. public function user()
  14. {
  15. return $this->belongsTo(User::class, 'creator_id', 'id');
  16. }
  17. public function children(){
  18. return $this->hasMany( Category::class, 'parent_id' );
  19. }
  20. public function parent() {
  21. return $this->belongsTo(Category::class);
  22. }
  23. public function products()
  24. {
  25. return $this->morphedByMany(Product::class, 'categorizable');
  26. }
  27. public function getCategoryTypeAttribute($value)
  28. {
  29. switch ($value) {
  30. case 0:
  31. $category_type = 'محصولات';
  32. break;
  33. case 1:
  34. $category_type = 'اخبار';
  35. break;
  36. case 2:
  37. $category_type = 'مطالب';
  38. break;
  39. default:
  40. $category_type = '';
  41. }
  42. return $category_type;
  43. }
  44. public function sluggable()
  45. {
  46. return [
  47. 'slug' => [
  48. 'source' => 'title'
  49. ]
  50. ];
  51. }
  52. }