Nation.php 3.3 KB

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