Nation.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. $this->emit('load-data-table');
  31. }
  32. public function render()
  33. {
  34. $this->records = \App\Models\Nation::select('id', 'name', 'code', 'enabled')->get();//->orderBy($this->sortField, $this->sortAsc ? 'asc' : 'desc')->get();
  35. return view('livewire.nation');
  36. }
  37. public function add()
  38. {
  39. $this->resetFields();
  40. $this->add = true;
  41. $this->update = false;
  42. }
  43. public function store()
  44. {
  45. $this->validate();
  46. try {
  47. \App\Models\Nation::create([
  48. 'name' => $this->name,
  49. 'code' => $this->code,
  50. //'enabled' => $this->enabled,
  51. //'is_italy' => $this->isItaly,
  52. ]);
  53. session()->flash('success','Nazione creata');
  54. $this->resetFields();
  55. $this->add = false;
  56. } catch (\Exception $ex) {
  57. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  58. }
  59. }
  60. public function edit($id){
  61. try {
  62. $nation = \App\Models\Nation::findOrFail($id);
  63. if( !$nation) {
  64. session()->flash('error','Nazione non trovata');
  65. } else {
  66. $this->name = $nation->name;
  67. $this->code = $nation->code;
  68. $this->enabled = $nation->enabled;
  69. $this->isItaly = $nation->is_italy;
  70. $this->dataId = $nation->id;
  71. $this->update = true;
  72. $this->add = false;
  73. }
  74. } catch (\Exception $ex) {
  75. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  76. }
  77. }
  78. public function update()
  79. {
  80. $this->validate();
  81. try {
  82. \App\Models\Nation::whereId($this->dataId)->update([
  83. 'name' => $this->name,
  84. 'code' => $this->code,
  85. 'enabled' => $this->enabled,
  86. 'is_italy' => $this->isItaly,
  87. ]);
  88. session()->flash('success','Nazione aggiornata');
  89. $this->resetFields();
  90. $this->update = false;
  91. } catch (\Exception $ex) {
  92. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  93. }
  94. }
  95. public function cancel()
  96. {
  97. $this->add = false;
  98. $this->update = false;
  99. $this->resetFields();
  100. }
  101. public function delete($id)
  102. {
  103. try{
  104. \App\Models\Nation::find($id)->delete();
  105. session()->flash('success',"Nazione eliminata");
  106. }catch(\Exception $e){
  107. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  108. }
  109. }
  110. }