Course.php 2.3 KB

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