Province.php 3.1 KB

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