Course.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. ];
  32. public function parent()
  33. {
  34. return $this->belongsTo(Course::class);
  35. }
  36. public function type()
  37. {
  38. return $this->belongsTo(CourseType::class, 'course_type_id');
  39. }
  40. public function duration()
  41. {
  42. return $this->belongsTo(CourseDuration::class, 'course_duration_id');
  43. }
  44. public function frequency()
  45. {
  46. return $this->belongsTo(CourseFrequency::class, 'course_frequency_id');
  47. }
  48. public function level()
  49. {
  50. return $this->belongsTo(CourseLevel::class, 'course_level_id');
  51. }
  52. public function category()
  53. {
  54. return $this->belongsTo(Category::class, 'category_id');
  55. }
  56. public function member()
  57. {
  58. return $this->belongsTo(Member::class);
  59. }
  60. public function childs() {
  61. return $this->hasMany(\App\Models\Course::class,'parent_id','id') ;
  62. }
  63. public function getTree()
  64. {
  65. $str = '';
  66. if ($this->parent_id != null)
  67. {
  68. $a = $this->recursiveName($this->parent_id, array($this->name));
  69. $a = array_reverse($a);
  70. $str = implode(" - ", $a);
  71. }
  72. else
  73. {
  74. $str = $this->name;
  75. }
  76. return $str;
  77. }
  78. public function recursiveName($parent_id, $array)
  79. {
  80. $x = \App\Models\Course::findOrFail($parent_id);
  81. $array[] = $x->name;
  82. if ($x->parent_id != null)
  83. {
  84. return $this->recursiveName($x->parent_id, $array);
  85. }
  86. else
  87. {
  88. return $array;
  89. }
  90. }
  91. public function recursiveParent($parent_id, $array)
  92. {
  93. if ($parent_id == null)
  94. return $array;
  95. $x = \App\Models\Course::findOrFail($parent_id);
  96. $array[] = $x->id;
  97. if ($x->parent_id != null)
  98. {
  99. return $this->recursiveParent($x->parent_id, $array);
  100. }
  101. else
  102. {
  103. return $array;
  104. }
  105. }
  106. public function getCount()
  107. {
  108. return \App\Models\MemberCourse::where('course_id', $this->id)->count();
  109. }
  110. }