LARAVEL - show name of the user when logged in

2019-09-20 15:49发布

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>

5条回答
姐就是有狂的资本
2楼-- · 2019-09-20 16:26

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

查看更多
Bombasti
3楼-- · 2019-09-20 16:29

You've said Auth::user()->name doesn't work for you and that's you're keeping password as is without hashing it. Also, it looks like you're using multiple tables to store users. In this case, Auth::check(), @guest, @auth and other directives will not work for you.

You need to add a new a new guard, modify some Laravel code and login users manually. But since you're new to Laravel, I think the easier solution for you will be checking users manually:

$admin = DB::table('admin')->where(['email' => $email, 'password' => $password])->first();
if (empty($admin)) {
    return redirect()->route('admin-login')->with('error', 'Invalid email or Password!');
}

You can also pass the variable to a view and share it between requests using session:

session(['user' => $admin]);

And do this instead of @auth:

if (session('user'))

And this instead of @guest:

if (empty(session('user')))

And to show admin name:

{{ session('user')->name }}

If you also want to avoid an error when there is no user, use the optional() helper:

{{ optional(session('user'))->name }}

It will print a name if a user is he is authenticated and will print nothing if he is not.

And I'd really recommend you to hash passwords with brcypt() helper and use one table for all users and just assign roles to the users. In this case, you'll be able to use built-in auth system and users' credentials will be safe.

查看更多
Animai°情兽
4楼-- · 2019-09-20 16:34

Try to use this in the controller:

return view('admin', ['name' => $checkLogin]);

Then in the views, you need to create a foreach loop:

@foreach($checkLogin as $key => $value)
    <p> {{ $value->email }} </p>
    <p> {{ $value->name }} </p>

@endforeach

Also, you can use this if you're using laravel's default auth:

{{ Auth::user()->name }}
查看更多
太酷不给撩
5楼-- · 2019-09-20 16:36

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

And his name like

Auth::user()->name;

查看更多
爷、活的狠高调
6楼-- · 2019-09-20 16:41

try using the below code :

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

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

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

    else {
       // echo "Login Failed!";
       return Redirect::route('admin-login')->with(['error'=> "Invalid email or Password!!"]);
    }
}
查看更多
登录 后发表回答