Course.php 2.7 KB

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