City.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. use App\Http\Middleware\TenantMiddleware;
  5. use Livewire\WithPagination;
  6. class City extends Component
  7. {
  8. use WithPagination;
  9. protected $paginationTheme = 'bootstrap';
  10. public $sortField ='cities.name';
  11. public $sortAsc = true;
  12. public function sortBy($field)
  13. {
  14. if($this->sortField === $field)
  15. {
  16. $this->sortAsc = ! $this->sortAsc;
  17. } else {
  18. $this->sortAsc = true;
  19. }
  20. $this->sortField = $field;
  21. }
  22. public $name, $enabled, $dataId, $province_id, $update = false, $add = false;
  23. public $provinces = array();
  24. protected $rules = [
  25. 'name' => 'required',
  26. 'province_id' => 'required'
  27. ];
  28. protected $messages = [
  29. 'name.required' => 'Il nome è obbligatorio'
  30. ];
  31. public function resetFields(){
  32. $this->name = '';
  33. $this->enabled = true;
  34. $this->province_id = null;
  35. }
  36. public function boot()
  37. {
  38. app(TenantMiddleware::class)->setupTenantConnection();
  39. }
  40. public function mount()
  41. {
  42. $this->provinces = \App\Models\Province::select('id', 'name')->get();
  43. }
  44. public function render()
  45. {
  46. $rows = \App\Models\City::select('cities.name', 'cities.id', 'cities.enabled', 'provinces.name as province_name')->join('provinces', 'cities.province_id', '=', 'provinces.id')->get();
  47. //->orderBy($this->sortField, $this->sortAsc ? 'asc' : 'desc')
  48. //->paginate(10);
  49. //$rows = \App\Models\City::with('province')->orderBy($this->sortField, $this->sortAsc ? 'asc' : 'desc')->paginate(10);
  50. return view('livewire.city', ['records' => $rows]);
  51. }
  52. public function add()
  53. {
  54. $this->resetFields();
  55. $this->add = true;
  56. $this->update = false;
  57. }
  58. public function store()
  59. {
  60. $this->validate();
  61. try {
  62. \App\Models\City::create([
  63. 'name' => $this->name,
  64. 'province_id' => $this->province_id,
  65. 'enabled' => $this->enabled
  66. ]);
  67. session()->flash('success','Città creata');
  68. $this->resetFields();
  69. $this->add = false;
  70. } catch (\Exception $ex) {
  71. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  72. }
  73. }
  74. public function edit($id){
  75. try {
  76. $city = \App\Models\City::findOrFail($id);
  77. if( !$city) {
  78. session()->flash('error','Città non trovata');
  79. } else {
  80. $this->name = $city->name;
  81. $this->enabled = $city->enabled;
  82. $this->province_id = $city->province_id;
  83. $this->dataId = $city->id;
  84. $this->update = true;
  85. $this->add = false;
  86. }
  87. } catch (\Exception $ex) {
  88. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  89. }
  90. }
  91. public function update()
  92. {
  93. $this->validate();
  94. try {
  95. \App\Models\City::whereId($this->dataId)->update([
  96. 'name' => $this->name,
  97. 'province_id' => $this->province_id,
  98. 'enabled' => $this->enabled
  99. ]);
  100. session()->flash('success','Città aggiornata');
  101. $this->resetFields();
  102. $this->update = false;
  103. } catch (\Exception $ex) {
  104. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  105. }
  106. }
  107. public function cancel()
  108. {
  109. $this->add = false;
  110. $this->update = false;
  111. $this->resetFields();
  112. }
  113. public function delete($id)
  114. {
  115. try{
  116. \App\Models\City::find($id)->delete();
  117. session()->flash('success',"Città eliminata");
  118. }catch(\Exception $e){
  119. session()->flash('error','Errore (' . $e->getMessage() . ')');
  120. }
  121. }
  122. }