setupTenantConnection(); $this->logCurrentDatabase('After tenant connection setup in boot()'); } public $records, $name, $cognome, $email, $password, $oldPassword, $level, $enabled, $dataId, $update = false, $add = false; public $userExists = 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', ]; /** * Helper method to log current database information */ private function logCurrentDatabase($context = '') { try { $currentConnection = DB::getDefaultConnection(); $currentDatabase = DB::connection()->getDatabaseName(); $user = Auth::user(); Log::info('Database Connection Info', [ 'context' => $context, 'current_connection' => $currentConnection, 'current_database' => $currentDatabase, 'user_id' => $user ? $user->id : null, 'user_tenant_database' => $user ? $user->tenant_database : null, 'user_tenant_username' => $user ? $user->tenant_username : null, ]); } catch (\Exception $e) { Log::error('Failed to get database info', [ 'context' => $context, 'error' => $e->getMessage() ]); } } 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->logCurrentDatabase('Before fetching users in render()'); $this->records = \App\Models\User::select('id', 'name', 'cognome', 'email', 'password', 'level', 'enabled')->get(); $this->logCurrentDatabase('After fetching users in render()'); return view('livewire.user'); } public function add() { $this->logCurrentDatabase('In add() method'); $this->resetFields(); $this->add = true; $this->update = false; $this->enabled = true; $this->userExists = false; } public function store() { $this->logCurrentDatabase('Start of store() method'); Log::info('User store', [ 'name' => $this->name, 'cognome' => $this->cognome, 'email' => $this->email, 'level' => $this->level, 'enabled' => $this->enabled ]); $rules = [ 'name' => 'required', 'cognome' => 'required', 'email' => 'required|email|unique:users,email', 'password' => 'required|min:6' ]; $messages = [ 'name.required' => 'Il nome è obbligatorio', 'cognome.required' => 'Il cognome è obbligatorio', 'email.required' => 'La mail è obbligatoria', 'email.email' => 'La mail deve essere un indirizzo valido', 'email.unique' => 'Questa mail è già stata utilizzata', 'password.required' => 'La password è obbligatoria', 'password.min' => 'La password deve essere di almeno 6 caratteri' ]; $this->validate($rules, $messages); $this->logCurrentDatabase('Before creating user in store()'); try { $user = \App\Models\User::create([ 'name' => $this->name, 'cognome' => $this->cognome, 'email' => $this->email, 'password' => bcrypt($this->password), 'level' => $this->level, 'enabled' => $this->enabled ]); $this->logCurrentDatabase('After creating user in store()'); Log::info('User created successfully', [ 'user_id' => $user->id, 'name' => $this->name, 'cognome' => $this->cognome, 'email' => $this->email, 'level' => $this->level, 'enabled' => $this->enabled, 'database' => DB::connection()->getDatabaseName() ]); session()->flash('success', 'Utente creato'); $this->resetFields(); $this->add = false; } catch (\Exception $ex) { $this->logCurrentDatabase('Error in store() method'); Log::error('User creation failed', [ 'error' => $ex->getMessage(), 'database' => DB::connection()->getDatabaseName(), 'user_data' => [ 'name' => $this->name, 'cognome' => $this->cognome, 'email' => $this->email, 'level' => $this->level, 'enabled' => $this->enabled ] ]); session()->flash('error', 'Errore (' . $ex->getMessage() . ')'); } } public function edit($id) { $this->logCurrentDatabase('Start of edit() method'); try { $user = \App\Models\User::findOrFail($id); $this->logCurrentDatabase('After finding user in edit()'); if (!$user) { session()->flash('error', 'Dato non trovato'); } else { $this->name = $user->name; $this->cognome = $user->cognome; $this->email = $user->email; $this->level = $user->level; $this->dataId = $user->id; $this->update = true; $this->add = false; $this->enabled = $user->enabled; $this->userExists = true; } Log::info('User edit loaded', [ 'user_id' => $id, 'name' => $this->name, 'cognome' => $this->cognome, 'email' => $this->email, 'level' => $this->level, 'database' => DB::connection()->getDatabaseName() ]); } catch (\Exception $ex) { $this->logCurrentDatabase('Error in edit() method'); Log::error('User edit failed', [ 'user_id' => $id, 'error' => $ex->getMessage(), 'database' => DB::connection()->getDatabaseName() ]); session()->flash('error', 'Errore (' . $ex->getMessage() . ')'); } } public function update() { $this->logCurrentDatabase('Start of update() method'); $this->validate(); try { \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 ]); $this->logCurrentDatabase('After updating user'); Log::info('User updated successfully', [ 'user_id' => $this->dataId, 'name' => $this->name, 'cognome' => $this->cognome, 'email' => $this->email, 'level' => $this->level, 'enabled' => $this->enabled, 'database' => DB::connection()->getDatabaseName() ]); session()->flash('success', 'Dato aggiornato'); $this->resetFields(); $this->update = false; } catch (\Exception $ex) { $this->logCurrentDatabase('Error in update() method'); Log::error('User update failed', [ 'user_id' => $this->dataId, 'error' => $ex->getMessage(), 'database' => DB::connection()->getDatabaseName() ]); session()->flash('error', 'Errore (' . $ex->getMessage() . ')'); } } public function cancel() { $this->logCurrentDatabase('In cancel() method'); $this->resetFields(); $this->add = false; $this->update = false; $this->userExists = false; $this->enabled = false; } public function delete($id) { $this->logCurrentDatabase('Start of delete() method'); try { \App\Models\User::find($id)->delete(); $this->logCurrentDatabase('After deleting user'); Log::info('User deleted successfully', [ 'user_id' => $id, 'database' => DB::connection()->getDatabaseName() ]); session()->flash('success', "Dato eliminato"); } catch (\Exception $e) { $this->logCurrentDatabase('Error in delete() method'); Log::error('User deletion failed', [ 'user_id' => $id, 'error' => $e->getMessage(), 'database' => DB::connection()->getDatabaseName() ]); session()->flash('error', 'Errore (' . $e->getMessage() . ')'); } } }