you can do something like
$user = User::find(1);
//ore use your own way to get the user
Auth::login($user);
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I am working on a medium sized application where the admins need to functionally to view the user side of the site with user permissions. Right now a 'user' is any role without the 'sys_user' permission.
Has anyone accomplished this functionality? If so, how did you do it? Are there any caveats?
Here's my solution:
public function impersonate()
{
if ($id = $this->session->get('impersonate_member'))
{
$this->onceUsingId($id);
return true;
}
return false;
}
public function isImpersonating()
{
return $this->can('sys_user') && $this->session->has('impersonate_member');
}
public function setUserToImpersonate(User $user)
{
$this->session->put('impersonate_member', $user->profile_slug);
}
public function postImpersonate()
{
$memberId = $this->request->get('member_id');
$user = \Repo::users()->findOrFail($memberId);
$this->auth->setUserToImpersonate($user);
return $this->handler->route(['account.profile', $memberId]);
}
public function filter($route, $request)
{
if (\Auth::isImpersonating()) \Auth::impersonate();
}
Please or to participate in this conversation.