Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LARAVEL - show name of the user when logged in

I have made a login page 'admin-login' which redirected to a page 'admin' when authenticated. I want to display the name of the logged in user on the 'admin' page.

Have a look at my code: Controller:

public function login(Request $req) {
    $email = $req->input('email');
    $password = $req->input('password');

    $checkLogin = DB::table('admin')->where(['email'=>$email,'password'=>$password])->get();

    if(count($checkLogin) > 0) {
        return view('admin')->with(['name'=>" "]);
    }

    else {
       // echo "Login Failed!";
       return Redirect::route('admin-login')->with(['error'=> "Invalid email or Password!!"]);
    }
}

I don't know what to write here 'with(['name'=>" "]);'. Plz help me out.

View:

<ul class="nav navbar-nav navbar-right">
    <!-- Authentication Links -->
    @guest
        <li><a href="{{ route('login') }}">Login</a></li>
        <li><a href="{{ route('register') }}">Register</a></li>
    @else
        <li class="dropdown">
            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false" aria-haspopup="true">
                {{ Session::get('name') }} <span class="caret"></span>
            </a>
        </li>
</ul>
like image 466
user9332352 Avatar asked Dec 02 '25 09:12

user9332352


2 Answers

You can get the currently authenticated user via Auth::user()

And his name like

Auth::user()->name;

like image 63
Mahdi Younesi Avatar answered Dec 04 '25 22:12

Mahdi Younesi


if you using laravel default auth model you can simply use {{Auth::user()->name}} it will print the name of the logedin user.

like image 25
Anil Kumar Sahu Avatar answered Dec 04 '25 22:12

Anil Kumar Sahu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!