| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- <?php
- namespace App\Http\Livewire;
- use Illuminate\Support\Facades\Log;
- use Livewire\Component;
- class User extends Component
- {
- public $records, $name,$cognome, $email, $password, $oldPassword, $level, $enabled, $dataId, $update = false, $add = false;
- protected $rules = [
- 'name' => 'required',
- 'cognome' => 'required',
- 'email' => 'required',
- 'password' => 'required'
- ];
- protected $messages = [
- 'name.required' => 'Il nome è obbligatorio',
- 'cognome.required' => 'Il cognome è obbligatorio',
- 'email.required' => 'La mail è obbligatoria',
- 'password.required' => 'La password è obbligatoria',
- ];
- public function resetFields(){
- $this->name = '';
- $this->cognome = '';
- $this->email = '';
- $this->password = '';
- $this->oldPassword = '';
- $this->level = 0;
- $this->enabled = true;
- $this->emit('load-data-table');
- }
- public function render()
- {
- $this->records = \App\Models\User::select('id', 'name','cognome' ,'email', 'password', 'level', 'enabled')->get();
- return view('livewire.user');
- }
- public function add()
- {
- $this->resetFields();
- $this->add = true;
- $this->update = false;
- }
- public function store()
- {
- Log::info('User store', [
- 'name' => $this->name,
- 'cognome' => $this->cognome,
- 'email' => $this->email,
- 'level' => $this->level,
- 'enabled' => $this->enabled
- ]);
- $this->validate();
- Log::info('User store', [
- 'name' => $this->name,
- 'cognome' => $this->cognome,
- 'email' => $this->email,
- 'level' => $this->level,
- 'enabled' => $this->enabled
- ]);
- try {
- \App\Models\User::create([
- 'name' => $this->name,
- 'cognome' => $this->cognome,
- 'email' => $this->email,
- 'password' => bcrypt($this->password),
- 'level' => $this->level,
- 'enabled' => $this->enabled
- ]);
- Log::info('User created', [
- 'name' => $this->name,
- 'cognome' => $this->cognome,
- 'email' => $this->email,
- 'level' => $this->level,
- 'enabled' => $this->enabled
- ]);
- session()->flash('success','Dato creato');
- $this->resetFields();
- $this->add = false;
- } catch (\Exception $ex) {
- session()->flash('error','Errore (' . $ex->getMessage() . ')');
- }
- }
- public function edit($id){
- try {
- $user = \App\Models\User::findOrFail($id);
- if( !$user) {
- session()->flash('error','Dato non trovato');
- } else {
- $this->name = $user->name;
- $this->cognome = $user->cognome;
- $this->email = $user->email;
- $this->password = $user->password;
- $this->level = $user->level;
- $this->dataId = $user->id;
- $this->update = true;
- $this->add = false;
- }
- Log::info('User edit', [
- 'name' => $this->name,
- 'cognome' => $this->cognome,
- 'email' => $this->email,
- 'level' => $this->level
- ]);
- } catch (\Exception $ex) {
- session()->flash('error','Errore (' . $ex->getMessage() . ')');
- }
- }
- public function update()
- {
- $this->validate();
- try {
- if ($this->pa)
- \App\Models\User::whereId($this->dataId)->update([
- 'name' => $this->name,
- 'cognome' => $this->cognome,
- 'email' => $this->email,
- 'password' => bcrypt($this->password),
- 'level' => $this->level,
- 'enabled' => $this->enabled
- ]);
- Log::info('User updated', [
- 'name' => $this->name,
- 'cognome' => $this->cognome,
- 'email' => $this->email,
- 'level' => $this->level,
- 'enabled' => $this->enabled
- ]);
- session()->flash('success','Dato aggiornato');
- $this->resetFields();
- $this->update = false;
- } catch (\Exception $ex) {
- session()->flash('error','Errore (' . $ex->getMessage() . ')');
- }
- }
- public function cancel()
- {
- $this->add = false;
- $this->update = false;
- $this->resetFields();
- }
- public function delete($id)
- {
- try{
- \App\Models\User::find($id)->delete();
- session()->flash('success',"Dato eliminato");
- }catch(\Exception $e){
- session()->flash('error','Errore (' . $e->getMessage() . ')');
- }
- }
- }
|