Stradario.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. use Livewire\WithPagination;
  5. class Stradario extends Component
  6. {
  7. use WithPagination;
  8. protected $paginationTheme = 'bootstrap';
  9. public $title = 'Stradario';
  10. public $search, $codicetoponimo, $toponimo, $descrizione, $dataId, $update = false, $add = false;
  11. protected $rules = [
  12. 'descrizione' => 'required'
  13. ];
  14. protected $messages = [
  15. 'descrizione.required' => 'Il nome è obbligatorio'
  16. ];
  17. public function resetFields(){
  18. $this->descrizione = '';
  19. $this->codicetoponimo = 0;
  20. $this->toponimo = '';
  21. }
  22. public function updatingSearch()
  23. {
  24. $this->resetPage();
  25. }
  26. public function render()
  27. {
  28. $rows = \App\Models\Stradario::where('descrizione', 'like', '%'.$this->search.'%')->orderBy('descrizione')->paginate(10);
  29. return view('livewire.stradario', ['records' => $rows]);
  30. }
  31. public function add()
  32. {
  33. $this->resetFields();
  34. $this->add = true;
  35. $this->update = false;
  36. }
  37. public function store()
  38. {
  39. $this->validate();
  40. try {
  41. \App\Models\Stradario::create([
  42. 'descrizione' => $this->descrizione,
  43. 'codicetoponimo' => $this->codicetoponimo,
  44. 'toponimo' => $this->toponimo,
  45. ]);
  46. session()->flash('success','Record creato');
  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. $record = \App\Models\Stradario::findOrFail($id);
  56. if( !$record) {
  57. session()->flash('error','Record non trovato');
  58. } else {
  59. $this->descrizione = $record->DESCRIZIONE;
  60. $this->codicetoponimo = $record->CODICETOPONIMO;
  61. $this->toponimo = $record->TOPONIMO;
  62. $this->dataId = $record->id;
  63. $this->update = true;
  64. $this->add = false;
  65. }
  66. } catch (\Exception $ex) {
  67. session()->flash('error','Errore');
  68. }
  69. }
  70. public function update()
  71. {
  72. $this->validate();
  73. try {
  74. \App\Models\Stradario::whereId($this->dataId)->update([
  75. 'descrizione' => $this->descrizione,
  76. 'codicetoponimo' => $this->codicetoponimo,
  77. 'toponimo' => $this->toponimo,
  78. ]);
  79. session()->flash('success','Record aggiornato');
  80. $this->resetFields();
  81. $this->update = false;
  82. } catch (\Exception $ex) {
  83. session()->flash('success','Errore');
  84. }
  85. }
  86. public function cancel()
  87. {
  88. $this->add = false;
  89. $this->update = false;
  90. $this->resetFields();
  91. }
  92. public function delete($id)
  93. {
  94. try{
  95. \App\Models\Stradario::find($id)->delete();
  96. session()->flash('success',"Record eliminato");
  97. }catch(\Exception $e){
  98. session()->flash('error',"Errore");
  99. }
  100. }
  101. }