Categories.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. class Categories 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 function render()
  13. {
  14. $reset = false;
  15. if ($this->level_1_id > 0)
  16. {
  17. $this->level_2 = \App\Models\Category::where('parent_id', $this->level_1_id)->orderBy('name')->get();
  18. if (sizeof($this->level_2) == 0)
  19. {
  20. $this->emit('storeCategoryWithID', $this->level_1_id);
  21. $reset = true;
  22. }
  23. }
  24. if ($this->level_2_id > 0)
  25. {
  26. $this->level_3 = \App\Models\Category::where('parent_id', $this->level_2_id)->orderBy('name')->get();
  27. if (sizeof($this->level_3) == 0)
  28. {
  29. $this->emit('storeCategoryWithID', $this->level_2_id);
  30. $reset = true;
  31. }
  32. }
  33. if ($this->level_3_id > 0)
  34. {
  35. $this->emit('storeCategoryWithID', $this->level_3_id);
  36. $reset = true;
  37. }
  38. $this->level_1 = \App\Models\Category::where('parent_id', null)->orderBy('name')->get();
  39. if ($reset)
  40. {
  41. $this->level_1_id = 0;
  42. $this->level_2_id = 0;
  43. $this->level_3_id = 0;
  44. $this->level_2 = [];
  45. $this->level_3 = [];
  46. }
  47. return view('livewire.categories');
  48. }
  49. }