Course.php 2.5 KB

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