Courses.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. class Courses extends Component
  5. {
  6. public $level_1 = [];
  7. public $level_2 = [];
  8. public $level_3 = [];
  9. public $level_1_id = 0;
  10. public $level_2_id = 0;
  11. public $level_3_id = 0;
  12. public $course_id = null;
  13. public function mount($course_id)
  14. {
  15. $this->course_id = $course_id;
  16. if ($this->course_id != null)
  17. {
  18. $c = \App\Models\Course::findOrFail($this->course_id);
  19. $ids = array_reverse($c->recursiveParent($c->parent_id, [$c->id]));
  20. foreach($ids as $ii => $i)
  21. {
  22. if ($ii == 0)
  23. {
  24. $this->level_1_id = $i;
  25. }
  26. if ($ii == 1)
  27. {
  28. $this->level_2_id = $i;
  29. }
  30. if ($ii == 2)
  31. {
  32. $this->level_3_id = $i;
  33. }
  34. }
  35. }
  36. }
  37. public function updatedLevel1Id() {
  38. $this->emit('setCourse', null);
  39. $this->level_2_id = 0;
  40. $this->level_2 = [];
  41. $this->level_3_id = 0;
  42. $this->level_3 = [];
  43. }
  44. public function updatedLevel2Id() {
  45. $this->emit('setCourse', null);
  46. $this->level_3_id = 0;
  47. $this->level_3 = [];
  48. }
  49. public function updatedLevel3Id() {
  50. $this->emit('setCourse', null);
  51. }
  52. public function render()
  53. {
  54. $reset = false;
  55. if ($this->level_1_id > 0)
  56. {
  57. $this->level_2 = \App\Models\Course::where('parent_id', $this->level_1_id)->orderBy('name')->get();
  58. if (sizeof($this->level_2) == 0)
  59. {
  60. $this->emit('setCourse', $this->level_1_id);
  61. $reset = true;
  62. }
  63. }
  64. if ($this->level_2_id > 0)
  65. {
  66. $this->level_3 = \App\Models\Course::where('parent_id', $this->level_2_id)->orderBy('name')->get();
  67. if (sizeof($this->level_3) == 0)
  68. {
  69. $this->emit('setCourse', $this->level_2_id);
  70. $reset = true;
  71. }
  72. }
  73. if ($this->level_3_id > 0)
  74. {
  75. $this->emit('setCourse', $this->level_3_id);
  76. $reset = true;
  77. }
  78. $this->level_1 = \App\Models\Course::where('parent_id', null)->orderBy('name')->get();
  79. return view('livewire.courses');
  80. }
  81. }