Course.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Factories\HasFactory;
  4. use Illuminate\Database\Eloquent\Model;
  5. class Course extends Model
  6. {
  7. use HasFactory;
  8. protected $fillable = [
  9. 'parent_id',
  10. 'name',
  11. 'course_type_id',
  12. 'causal_id',
  13. 'max_members',
  14. 'instructor',
  15. 'price',
  16. 'months',
  17. 'date_from',
  18. 'date_to',
  19. 'course_duration_id',
  20. 'course_frequency_id',
  21. 'course_level_id',
  22. 'enabled',
  23. 'year',
  24. 'subscription_price',
  25. 'sub_causal_id',
  26. 'category_id',
  27. 'active',
  28. 'type',
  29. 'when',
  30. 'prices',
  31. 'instructor_id',
  32. 'color'
  33. ];
  34. public function parent()
  35. {
  36. return $this->belongsTo(Course::class);
  37. }
  38. public function type()
  39. {
  40. return $this->belongsTo(CourseType::class, 'course_type_id');
  41. }
  42. public function duration()
  43. {
  44. return $this->belongsTo(CourseDuration::class, 'course_duration_id');
  45. }
  46. public function frequency()
  47. {
  48. return $this->belongsTo(CourseFrequency::class, 'course_frequency_id');
  49. }
  50. public function level()
  51. {
  52. return $this->belongsTo(CourseLevel::class, 'course_level_id');
  53. }
  54. public function category()
  55. {
  56. return $this->belongsTo(Category::class, 'category_id');
  57. }
  58. public function member()
  59. {
  60. return $this->belongsTo(Member::class);
  61. }
  62. public function getins()
  63. {
  64. return $this->belongsTo(User::class, 'instructor_id');
  65. }
  66. public function childs() {
  67. return $this->hasMany(\App\Models\Course::class,'parent_id','id') ;
  68. }
  69. public function getTree()
  70. {
  71. $str = '';
  72. if ($this->parent_id != null)
  73. {
  74. $a = $this->recursiveName($this->parent_id, array($this->name));
  75. $a = array_reverse($a);
  76. $str = implode(" - ", $a);
  77. }
  78. else
  79. {
  80. $str = $this->name;
  81. }
  82. return $str;
  83. }
  84. public function recursiveName($parent_id, $array)
  85. {
  86. $x = \App\Models\Course::findOrFail($parent_id);
  87. $array[] = $x->name;
  88. if ($x->parent_id != null)
  89. {
  90. return $this->recursiveName($x->parent_id, $array);
  91. }
  92. else
  93. {
  94. return $array;
  95. }
  96. }
  97. public function recursiveParent($parent_id, $array)
  98. {
  99. if ($parent_id == null)
  100. return $array;
  101. $x = \App\Models\Course::findOrFail($parent_id);
  102. $array[] = $x->id;
  103. if ($x->parent_id != null)
  104. {
  105. return $this->recursiveParent($x->parent_id, $array);
  106. }
  107. else
  108. {
  109. return $array;
  110. }
  111. }
  112. public function getCount()
  113. {
  114. return \App\Models\MemberCourse::where('course_id', $this->id)->count();
  115. }
  116. }