| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- <?php
- namespace App\Http\Livewire;
- use Livewire\Component;
- class User extends Component
- {
- public $records, $name, $email, $password, $oldPassword, $level, $enabled, $dataId, $update = false, $add = false;
- protected $rules = [
- 'name' => 'required',
- 'email' => 'required',
- 'password' => 'required'
- ];
- protected $messages = [
- 'name.required' => 'Il nome è obbligatorio',
- 'email.required' => 'La mail è obbligatoria',
- 'password.required' => 'La password è obbligatoria',
- ];
- public function resetFields(){
- $this->name = '';
- $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', 'email', 'password', 'level', 'enabled')->get();
- return view('livewire.user');
- }
- public function add()
- {
- $this->resetFields();
- $this->add = true;
- $this->update = false;
- }
- public function store()
- {
- $this->validate();
- try {
- \App\Models\CourseDuration::create([
- 'name' => $this->name,
- 'email' => $this->email,
- 'password' => bcrypt($this->password),
- '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->email = $user->email;
- $this->password = $user->password;
- $this->level = $user->level;
- $this->dataId = $course_duration->id;
- $this->update = true;
- $this->add = false;
- }
- } 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,
- 'email' => $this->email,
- 'password' => bcrypt($this->password),
- '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 (' . $ex->getMessage() . ')');
- }
- }
- }
|