To get the currently logged in user you can inject the Authentication interface in your controller, or a view composer and using the following:
$user = $this->auth->check();
$user is false, means the current user isn't logged in. Otherwise you'll get the currently logged in user.
Check the method signature:
* Check if the user is logged in, if so return the current user
* @return bool|object
public function check();
Recommended: View Composer
The recommended way of attaching the
$currentUser variable to your desired views is by binding the
CurrentUserViewComposer to your view(s).
Let's say you want the current user on the
layouts.demo view, you can add the following code to your module's service provider:
Start by making your public controllers extend the following class:
Now, in any public view, you have access to a
$currentUser variable that corresponds to the currently authenticated user.
$currentUser will evaluate to false if none logged in.