City.php 3.7 KB

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