CourseDuration.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. }
  32. public function render()
  33. {
  34. $this->records = \App\Models\CourseDuration::select('id', 'name', 'duration', 'enabled')->get();
  35. return view('livewire.course_duration');
  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\CourseDuration::create([
  48. 'name' => $this->name,
  49. 'duration' => $this->duration,
  50. 'enabled' => $this->enabled
  51. ]);
  52. session()->flash('success','Dato creato');
  53. $this->resetFields();
  54. $this->add = false;
  55. } catch (\Exception $ex) {
  56. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  57. }
  58. }
  59. public function edit($id){
  60. try {
  61. $course_duration = \App\Models\CourseDuration::findOrFail($id);
  62. if( !$course_duration) {
  63. session()->flash('error','Dato non trovato');
  64. } else {
  65. $this->name = $course_duration->name;
  66. $this->duration = $course_duration->duration;
  67. $this->enabled = $course_duration->enabled;
  68. $this->dataId = $course_duration->id;
  69. $this->update = true;
  70. $this->add = false;
  71. }
  72. } catch (\Exception $ex) {
  73. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  74. }
  75. }
  76. public function update()
  77. {
  78. $this->validate();
  79. try {
  80. \App\Models\CourseDuration::whereId($this->dataId)->update([
  81. 'name' => $this->name,
  82. 'duration' => $this->duration,
  83. 'enabled' => $this->enabled
  84. ]);
  85. session()->flash('success','Dato aggiornato');
  86. $this->resetFields();
  87. $this->update = false;
  88. } catch (\Exception $ex) {
  89. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  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\CourseDuration::find($id)->delete();
  102. session()->flash('success',"Dato eliminato");
  103. }catch(\Exception $e){
  104. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  105. }
  106. }
  107. }