laraveloctobercmsoctobercms-plugins

How can I use a variable that belongs to rainlab.user plugin in my custom component


I'm building a website in October CMS, I installed rainlab.user plugin for user account management. Now I'm currently trying to create my custom component named "UserDetails.php". I am able to get the username in web page using {{ user.username }}. But I need to fetch that username in UserDetails.php. How can I do that? Could you please help me out? Thanks in Advance.!!


Solution

  • You can use Rainlab.user plugin's Auth facade to get current login user details.

    you can use below code

    ref: https://tutorialmeta.com/octobercms/october-cms-access-logged-user-rainlab-user-plugin

    use Auth;
    
    // it will returns the current logged in user
    $user = Auth::getUser(); 
    if($user) {
        dd($user->username);
    }
    
    // further more to check if any user logged in or not you can use 
    // it will return true if user is logged in
    $loggedIn = Auth::check();
    dd($loggedIn);
    
    

    if any doubt please comment.