User.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. class User extends Component
  5. {
  6. public $records, $name, $email, $password, $oldPassword, $level, $enabled, $dataId, $update = false, $add = false;
  7. protected $rules = [
  8. 'name' => 'required',
  9. 'email' => 'required',
  10. 'password' => 'required'
  11. ];
  12. protected $messages = [
  13. 'name.required' => 'Il nome è obbligatorio',
  14. 'email.required' => 'La mail è obbligatoria',
  15. 'password.required' => 'La password è obbligatoria',
  16. ];
  17. public function resetFields(){
  18. $this->name = '';
  19. $this->email = '';
  20. $this->password = '';
  21. $this->oldPassword = '';
  22. $this->level = 0;
  23. $this->enabled = true;
  24. $this->emit('load-data-table');
  25. }
  26. public function render()
  27. {
  28. $this->records = \App\Models\User::select('id', 'name', 'email', 'password', 'level', 'enabled')->get();
  29. return view('livewire.user');
  30. }
  31. public function add()
  32. {
  33. $this->resetFields();
  34. $this->add = true;
  35. $this->update = false;
  36. }
  37. public function store()
  38. {
  39. $this->validate();
  40. try {
  41. \App\Models\User::create([
  42. 'name' => $this->name,
  43. 'email' => $this->email,
  44. 'password' => bcrypt($this->password),
  45. 'level' => $this->level,
  46. 'enabled' => $this->enabled ? 1 : 0
  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. $user = \App\Models\User::findOrFail($id);
  58. if( !$user) {
  59. session()->flash('error','Dato non trovato');
  60. } else {
  61. $this->name = $user->name;
  62. $this->email = $user->email;
  63. $this->oldPassword = $user->password;
  64. $this->level = $user->level;
  65. $this->enabled = $user->enabled == 1;
  66. $this->dataId = $user->id;
  67. $this->update = true;
  68. $this->add = false;
  69. }
  70. } catch (\Exception $ex) {
  71. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  72. }
  73. }
  74. public function update()
  75. {
  76. $this->validate(
  77. [
  78. 'name' => 'required',
  79. 'email' => 'required',
  80. ]
  81. );
  82. try {
  83. \App\Models\User::whereId($this->dataId)->update([
  84. 'name' => $this->name,
  85. 'email' => $this->email,
  86. 'password' => $this->password != '' ? bcrypt($this->password) : $this->oldPassword,
  87. 'level' => $this->level,
  88. 'enabled' => $this->enabled ? 1 : 0
  89. ]);
  90. session()->flash('success','Dato aggiornato');
  91. $this->resetFields();
  92. $this->update = false;
  93. } catch (\Exception $ex) {
  94. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  95. }
  96. }
  97. public function cancel()
  98. {
  99. $this->add = false;
  100. $this->update = false;
  101. $this->resetFields();
  102. }
  103. public function delete($id)
  104. {
  105. try{
  106. \App\Models\User::find($id)->delete();
  107. session()->flash('success',"Dato eliminato");
  108. }catch(\Exception $e){
  109. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  110. }
  111. }
  112. }