City.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 $name, $enabled, $dataId, $province_id, $update = false, $add = false;
  10. public $provinces = array();
  11. protected $rules = [
  12. 'name' => 'required',
  13. 'province_id' => 'required'
  14. ];
  15. protected $messages = [
  16. 'name.required' => 'Il nome è obbligatorio'
  17. ];
  18. public function resetFields(){
  19. $this->name = '';
  20. $this->enabled = true;
  21. $this->province_id = null;
  22. }
  23. public function mount()
  24. {
  25. $this->provinces = \App\Models\Province::select('id', 'name')->get();
  26. }
  27. public function render()
  28. {
  29. $rows = \App\Models\City::with('province')->paginate(10);
  30. return view('livewire.city', ['records' => $rows]);
  31. }
  32. public function add()
  33. {
  34. $this->resetFields();
  35. $this->add = true;
  36. $this->update = false;
  37. }
  38. public function store()
  39. {
  40. $this->validate();
  41. try {
  42. \App\Models\City::create([
  43. 'name' => $this->name,
  44. 'province_id' => $this->province_id,
  45. 'enabled' => $this->enabled
  46. ]);
  47. session()->flash('success','Città creata');
  48. $this->resetFields();
  49. $this->add = false;
  50. } catch (\Exception $ex) {
  51. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  52. }
  53. }
  54. public function edit($id){
  55. try {
  56. $city = \App\Models\City::findOrFail($id);
  57. if( !$city) {
  58. session()->flash('error','Città non trovata');
  59. } else {
  60. $this->name = $city->name;
  61. $this->enabled = $city->enabled;
  62. $this->province_id = $city->province_id;
  63. $this->dataId = $city->id;
  64. $this->update = true;
  65. $this->add = false;
  66. }
  67. } catch (\Exception $ex) {
  68. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  69. }
  70. }
  71. public function update()
  72. {
  73. $this->validate();
  74. try {
  75. \App\Models\City::whereId($this->dataId)->update([
  76. 'name' => $this->name,
  77. 'province_id' => $this->province_id,
  78. 'enabled' => $this->enabled
  79. ]);
  80. session()->flash('success','Città aggiornata');
  81. $this->resetFields();
  82. $this->update = false;
  83. } catch (\Exception $ex) {
  84. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  85. }
  86. }
  87. public function cancel()
  88. {
  89. $this->add = false;
  90. $this->update = false;
  91. $this->resetFields();
  92. }
  93. public function delete($id)
  94. {
  95. try{
  96. \App\Models\City::find($id)->delete();
  97. session()->flash('success',"Città eliminata");
  98. }catch(\Exception $e){
  99. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  100. }
  101. }
  102. }