ModelloVeicolo.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. use Livewire\WithPagination;
  5. class ModelloVeicolo extends Component
  6. {
  7. use WithPagination;
  8. protected $paginationTheme = 'bootstrap';
  9. public $title = 'Modello veicolo';
  10. public $marche = [];
  11. public $search, $search_marca_id, $name, $marca_id, $dataId, $update = false, $add = false;
  12. protected $rules = [
  13. 'name' => 'required'
  14. ];
  15. protected $messages = [
  16. 'name.required' => 'Il nome è obbligatorio'
  17. ];
  18. public function resetFields(){
  19. $this->name = '';
  20. $this->marca_id = null;
  21. }
  22. public function updatingSearch()
  23. {
  24. $this->resetPage();
  25. }
  26. public function render()
  27. {
  28. $this->marche = \App\Models\MarcaVeicolo::orderBy('name')->get();
  29. $rows = \App\Models\ModelloVeicolo::where('name', 'like', '%'.$this->search.'%');
  30. $rows = $rows->orderBy('name')->paginate(10);
  31. return view('livewire.modello-veicolo', ['records' => $rows]);
  32. }
  33. public function add()
  34. {
  35. $this->resetFields();
  36. $this->add = true;
  37. $this->update = false;
  38. }
  39. public function store()
  40. {
  41. $this->validate();
  42. try {
  43. $m = \App\Models\ModelloVeicolo::create([
  44. 'name' => $this->name
  45. ]);
  46. \App\Models\MarcaModelloVeicolo::create([
  47. 'marca_id' => $this->marca_id,
  48. 'modello_id' => $m->id
  49. ]);
  50. session()->flash('success','Record creato');
  51. $this->resetFields();
  52. $this->add = false;
  53. } catch (\Exception $ex) {
  54. session()->flash('error','Errore in fase di salvataggio');
  55. }
  56. }
  57. public function edit($id){
  58. try {
  59. $record = \App\Models\ModelloVeicolo::findOrFail($id);
  60. if( !$record) {
  61. session()->flash('error','Record non trovato');
  62. } else {
  63. $this->name = $record->name;
  64. $this->dataId = $record->id;
  65. $this->update = true;
  66. $this->add = false;
  67. $this->marca_id = \App\Models\MarcaModelloVeicolo::where('modello_id', $this->dataId)->first()->marca_id;
  68. }
  69. } catch (\Exception $ex) {
  70. session()->flash('error','Errore');
  71. }
  72. }
  73. public function update()
  74. {
  75. $this->validate();
  76. try {
  77. \App\Models\ModelloVeicolo::whereId($this->dataId)->update([
  78. 'name' => $this->name
  79. ]);
  80. \App\Models\MarcaModelloVeicolo::where('modello_id', $this->dataId)->delete();
  81. \App\Models\MarcaModelloVeicolo::create([
  82. 'marca_id' => $this->marca_id,
  83. 'modello_id' => $this->dataId
  84. ]);
  85. session()->flash('success','Record aggiornato');
  86. $this->resetFields();
  87. $this->update = false;
  88. } catch (\Exception $ex) {
  89. session()->flash('success','Errore');
  90. }
  91. }
  92. public function cancel()
  93. {
  94. $this->add = false;
  95. $this->update = false;
  96. $this->resetFields();
  97. }
  98. public function delete($id)
  99. {
  100. try{
  101. \App\Models\ModelloVeicolo::find($id)->delete();
  102. session()->flash('success',"Record eliminato");
  103. }catch(\Exception $e){
  104. session()->flash('error',"Errore");
  105. }
  106. }
  107. }