Province.php 3.7 KB

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