Categories.php 1.5 KB

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