User.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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
  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->password = $user->password;
  64. $this->level = $user->level;
  65. $this->dataId = $user->id;
  66. $this->update = true;
  67. $this->add = false;
  68. }
  69. } catch (\Exception $ex) {
  70. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  71. }
  72. }
  73. public function update()
  74. {
  75. $this->validate();
  76. try {
  77. if ($this->pa)
  78. \App\Models\User::whereId($this->dataId)->update([
  79. 'name' => $this->name,
  80. 'email' => $this->email,
  81. 'password' => bcrypt($this->password),
  82. 'level' => $this->level,
  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\User::find($id)->delete();
  102. session()->flash('success',"Dato eliminato");
  103. }catch(\Exception $e){
  104. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  105. }
  106. }
  107. }