CourseDuration.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. class CourseDuration extends Component
  5. {
  6. public $records, $name, $duration, $enabled, $dataId, $update = false, $add = false;
  7. protected $rules = [
  8. 'name' => 'required',
  9. 'duration' => 'required'
  10. ];
  11. protected $messages = [
  12. 'name.required' => 'Il nome è obbligatorio',
  13. 'duration.required' => 'La durata è obbligatoria',
  14. ];
  15. public $sortField ='name';
  16. public $sortAsc = true;
  17. public function sortBy($field)
  18. {
  19. if($this->sortField === $field)
  20. {
  21. $this->sortAsc = ! $this->sortAsc;
  22. } else {
  23. $this->sortAsc = true;
  24. }
  25. $this->sortField = $field;
  26. }
  27. public function resetFields(){
  28. $this->name = '';
  29. $this->duration = 0;
  30. $this->enabled = true;
  31. $this->emit('load-data-table');
  32. }
  33. public function render()
  34. {
  35. $this->records = \App\Models\CourseDuration::select('id', 'name', 'duration', 'enabled')->get();
  36. return view('livewire.course_duration');
  37. }
  38. public function add()
  39. {
  40. $this->resetFields();
  41. $this->add = true;
  42. $this->update = false;
  43. }
  44. public function store()
  45. {
  46. $this->validate();
  47. try {
  48. \App\Models\CourseDuration::create([
  49. 'name' => $this->name,
  50. 'duration' => $this->duration,
  51. 'enabled' => $this->enabled
  52. ]);
  53. session()->flash('success','Dato creato');
  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. $course_duration = \App\Models\CourseDuration::findOrFail($id);
  63. if( !$course_duration) {
  64. session()->flash('error','Dato non trovato');
  65. } else {
  66. $this->name = $course_duration->name;
  67. $this->duration = $course_duration->duration;
  68. $this->enabled = $course_duration->enabled;
  69. $this->dataId = $course_duration->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\CourseDuration::whereId($this->dataId)->update([
  82. 'name' => $this->name,
  83. 'duration' => $this->duration,
  84. 'enabled' => $this->enabled
  85. ]);
  86. session()->flash('success','Dato aggiornato');
  87. $this->resetFields();
  88. $this->update = false;
  89. } catch (\Exception $ex) {
  90. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  91. }
  92. }
  93. public function cancel()
  94. {
  95. $this->add = false;
  96. $this->update = false;
  97. $this->resetFields();
  98. }
  99. public function delete($id)
  100. {
  101. try{
  102. \App\Models\CourseDuration::find($id)->delete();
  103. session()->flash('success',"Dato eliminato");
  104. }catch(\Exception $e){
  105. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  106. }
  107. }
  108. }