SegnalazionePervenutaDa.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. use Livewire\WithPagination;
  5. class SegnalazionePervenutaDa extends Component
  6. {
  7. use WithPagination;
  8. protected $paginationTheme = 'bootstrap';
  9. public $title = 'Segnalazione pervenuta da';
  10. public $search, $name, $para_agg, $dataId, $update = false, $add = false;
  11. protected $rules = [
  12. 'name' => 'required'
  13. ];
  14. protected $messages = [
  15. 'name.required' => 'Il nome è obbligatorio'
  16. ];
  17. public function resetFields(){
  18. $this->name = '';
  19. $this->para_agg = '';
  20. }
  21. public function updatingSearch()
  22. {
  23. $this->resetPage();
  24. }
  25. public function render()
  26. {
  27. $rows = \App\Models\SegnalazionePervenutaDa::where('name', 'like', '%'.$this->search.'%')->orderBy('name')->paginate(10);
  28. return view('livewire.segnalazione-pervenuta-da', ['records' => $rows]);
  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\SegnalazionePervenutaDa::create([
  41. 'name' => $this->name,
  42. 'para_agg' => $this->para_agg,
  43. ]);
  44. session()->flash('success','Record creato');
  45. $this->resetFields();
  46. $this->add = false;
  47. } catch (\Exception $ex) {
  48. session()->flash('error','Errore in fase di salvataggio');
  49. }
  50. }
  51. public function edit($id){
  52. try {
  53. $record = \App\Models\SegnalazionePervenutaDa::findOrFail($id);
  54. if( !$record) {
  55. session()->flash('error','Record non trovato');
  56. } else {
  57. $this->name = $record->name;
  58. $this->para_agg = $record->PARA_AGG;
  59. $this->dataId = $record->id;
  60. $this->update = true;
  61. $this->add = false;
  62. }
  63. } catch (\Exception $ex) {
  64. session()->flash('error','Errore');
  65. }
  66. }
  67. public function update()
  68. {
  69. $this->validate();
  70. try {
  71. \App\Models\SegnalazionePervenutaDa::whereId($this->dataId)->update([
  72. 'name' => $this->name,
  73. 'para_agg' => $this->para_agg,
  74. ]);
  75. session()->flash('success','Record aggiornato');
  76. $this->resetFields();
  77. $this->update = false;
  78. } catch (\Exception $ex) {
  79. session()->flash('success','Errore');
  80. }
  81. }
  82. public function cancel()
  83. {
  84. $this->add = false;
  85. $this->update = false;
  86. $this->resetFields();
  87. }
  88. public function delete($id)
  89. {
  90. try{
  91. \App\Models\SegnalazionePervenutaDa::find($id)->delete();
  92. session()->flash('success',"Record eliminato");
  93. }catch(\Exception $e){
  94. session()->flash('error',"Errore");
  95. }
  96. }
  97. }