Province.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. $this->emit('load-data-table');
  33. }
  34. public function mount()
  35. {
  36. $this->nations = \App\Models\Nation::select('id', 'name')->get();
  37. }
  38. public function render()
  39. {
  40. $this->records = \App\Models\Province::with('nation')->get();
  41. foreach($this->records as $r)
  42. {
  43. $r->nation = $r->nation->name;
  44. }
  45. /*if ($this->sortAsc)
  46. $this->records = $this->records->sortBy($this->sortField);
  47. else
  48. $this->records = $this->records->sortByDesc($this->sortField);*/
  49. return view('livewire.province');
  50. }
  51. public function add()
  52. {
  53. $this->resetFields();
  54. $this->add = true;
  55. $this->update = false;
  56. }
  57. public function store()
  58. {
  59. $this->validate();
  60. try {
  61. \App\Models\Province::create([
  62. 'name' => $this->name,
  63. 'code' => $this->code,
  64. 'nation_id' => $this->nation_id,
  65. 'enabled' => $this->enabled
  66. ]);
  67. session()->flash('success','Provincia 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. $province = \App\Models\Province::findOrFail($id);
  77. if( !$province) {
  78. session()->flash('error','Provincia non trovata');
  79. } else {
  80. $this->name = $province->name;
  81. $this->code = $province->code;
  82. $this->enabled = $province->enabled;
  83. $this->nation_id = $province->nation_id;
  84. $this->dataId = $province->id;
  85. $this->update = true;
  86. $this->add = false;
  87. }
  88. } catch (\Exception $ex) {
  89. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  90. }
  91. }
  92. public function update()
  93. {
  94. $this->validate();
  95. try {
  96. \App\Models\Province::whereId($this->dataId)->update([
  97. 'name' => $this->name,
  98. 'code' => $this->code,
  99. 'nation_id' => $this->nation_id,
  100. 'enabled' => $this->enabled
  101. ]);
  102. session()->flash('success','Provincia aggiornata');
  103. $this->resetFields();
  104. $this->update = false;
  105. } catch (\Exception $ex) {
  106. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  107. }
  108. }
  109. public function cancel()
  110. {
  111. $this->add = false;
  112. $this->update = false;
  113. $this->resetFields();
  114. }
  115. public function delete($id)
  116. {
  117. try{
  118. \App\Models\Province::find($id)->delete();
  119. session()->flash('success',"Provincia eliminata");
  120. }catch(\Exception $e){
  121. session()->flash('error','Errore (' . $e->getMessage() . ')');
  122. }
  123. }
  124. }