CourseSubscription.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. class CourseSubscription extends Component
  5. {
  6. public $records, $name, $months, $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->months = 0;
  28. $this->enabled = true;
  29. $this->emit('load-data-table');
  30. }
  31. public function render()
  32. {
  33. $this->records = \App\Models\CourseSubscription::select('id', 'name', 'enabled', 'months')->get();
  34. return view('livewire.course_subscription');
  35. }
  36. public function add()
  37. {
  38. $this->resetFields();
  39. $this->add = true;
  40. $this->update = false;
  41. }
  42. public function store()
  43. {
  44. $this->validate();
  45. try {
  46. \App\Models\CourseSubscription::create([
  47. 'name' => $this->name,
  48. 'months' => $this->months,
  49. 'enabled' => $this->enabled
  50. ]);
  51. session()->flash('success','Dato creato');
  52. $this->resetFields();
  53. $this->add = false;
  54. } catch (\Exception $ex) {
  55. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  56. }
  57. }
  58. public function edit($id){
  59. try {
  60. $course_subscription = \App\Models\CourseSubscription::findOrFail($id);
  61. if( !$course_subscription) {
  62. session()->flash('error','Dato non trovato');
  63. } else {
  64. $this->name = $course_subscription->name;
  65. $this->months = $course_subscription->months;
  66. $this->enabled = $course_subscription->enabled;
  67. $this->dataId = $course_subscription->id;
  68. $this->update = true;
  69. $this->add = false;
  70. }
  71. } catch (\Exception $ex) {
  72. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  73. }
  74. }
  75. public function update()
  76. {
  77. $this->validate();
  78. try {
  79. \App\Models\CourseSubscription::whereId($this->dataId)->update([
  80. 'name' => $this->name,
  81. 'months' => $this->months,
  82. 'enabled' => $this->enabled
  83. ]);
  84. session()->flash('success','Dato aggiornato');
  85. $this->resetFields();
  86. $this->update = false;
  87. } catch (\Exception $ex) {
  88. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  89. }
  90. }
  91. public function cancel()
  92. {
  93. $this->add = false;
  94. $this->update = false;
  95. $this->resetFields();
  96. }
  97. public function delete($id)
  98. {
  99. try{
  100. \App\Models\CourseSubscription::find($id)->delete();
  101. session()->flash('success',"Dato eliminato");
  102. }catch(\Exception $e){
  103. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  104. }
  105. }
  106. }