Categories.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. use App\Http\Middleware\TenantMiddleware;
  5. class Categories extends Component
  6. {
  7. public $level_1 = [];
  8. public $level_2 = [];
  9. public $level_3 = [];
  10. public $level_1_id = 0;
  11. public $level_2_id = 0;
  12. public $level_3_id = 0;
  13. public function boot()
  14. {
  15. app(TenantMiddleware::class)->setupTenantConnection();
  16. }
  17. public function render()
  18. {
  19. $this->emit('hideMsg');
  20. $reset = false;
  21. if ($this->level_1_id > 0)
  22. {
  23. $this->level_2 = \App\Models\Category::where('parent_id', $this->level_1_id)->orderBy('name')->get();
  24. if (sizeof($this->level_2) == 0)
  25. {
  26. $this->emit('storeCategoryWithID', $this->level_1_id);
  27. $reset = true;
  28. }
  29. }
  30. if ($this->level_2_id > 0)
  31. {
  32. $this->level_3 = \App\Models\Category::where('parent_id', $this->level_2_id)->orderBy('name')->get();
  33. if (sizeof($this->level_3) == 0)
  34. {
  35. $this->emit('storeCategoryWithID', $this->level_2_id);
  36. $reset = true;
  37. }
  38. }
  39. if ($this->level_3_id > 0)
  40. {
  41. $this->emit('storeCategoryWithID', $this->level_3_id);
  42. $reset = true;
  43. }
  44. $this->level_1 = \App\Models\Category::where('parent_id', null)->orderBy('name')->get();
  45. if ($reset)
  46. {
  47. $this->level_1_id = 0;
  48. $this->level_2_id = 0;
  49. $this->level_3_id = 0;
  50. $this->level_2 = [];
  51. $this->level_3 = [];
  52. }
  53. return view('livewire.categories');
  54. }
  55. }