CourseSubscription.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. class CourseSubscription extends Component
  5. {
  6. public $records, $name, $enabled, $dataId, $update = false, $add = false;
  7. protected $rules = [
  8. 'name' => 'required'
  9. ];
  10. protected $messages = [
  11. 'name.required' => 'Il nome è obbligatorio'
  12. ];
  13. public $sortField ='name';
  14. public $sortAsc = true;
  15. public function sortBy($field)
  16. {
  17. if($this->sortField === $field)
  18. {
  19. $this->sortAsc = ! $this->sortAsc;
  20. } else {
  21. $this->sortAsc = true;
  22. }
  23. $this->sortField = $field;
  24. }
  25. public function resetFields(){
  26. $this->name = '';
  27. $this->enabled = true;
  28. }
  29. public function render()
  30. {
  31. $this->records = \App\Models\CourseSubscription::select('id', 'name', 'enabled')->get();
  32. return view('livewire.course_subscription');
  33. }
  34. public function add()
  35. {
  36. $this->resetFields();
  37. $this->add = true;
  38. $this->update = false;
  39. }
  40. public function store()
  41. {
  42. $this->validate();
  43. try {
  44. \App\Models\CourseSubscription::create([
  45. 'name' => $this->name,
  46. 'enabled' => $this->enabled
  47. ]);
  48. session()->flash('success','Dato creato');
  49. $this->resetFields();
  50. $this->add = false;
  51. } catch (\Exception $ex) {
  52. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  53. }
  54. }
  55. public function edit($id){
  56. try {
  57. $course_subscription = \App\Models\CourseSubscription::findOrFail($id);
  58. if( !$course_subscription) {
  59. session()->flash('error','Dato non trovato');
  60. } else {
  61. $this->name = $course_subscription->name;
  62. $this->enabled = $course_subscription->enabled;
  63. $this->dataId = $course_subscription->id;
  64. $this->update = true;
  65. $this->add = false;
  66. }
  67. } catch (\Exception $ex) {
  68. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  69. }
  70. }
  71. public function update()
  72. {
  73. $this->validate();
  74. try {
  75. \App\Models\CourseSubscription::whereId($this->dataId)->update([
  76. 'name' => $this->name,
  77. 'enabled' => $this->enabled
  78. ]);
  79. session()->flash('success','Dato aggiornato');
  80. $this->resetFields();
  81. $this->update = false;
  82. } catch (\Exception $ex) {
  83. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  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\CourseSubscription::find($id)->delete();
  96. session()->flash('success',"Dato eliminato");
  97. }catch(\Exception $e){
  98. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  99. }
  100. }
  101. }