LARAVEL - 登录时显示用户名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48822084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:24:08  来源:igfitidea点击:

LARAVEL - show name of the user when logged in

phplaravellaravel-5

提问by user9332352

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.

我制作了一个登录页面“admin-login”,在通过身份验证后重定向到“admin”页面。我想在“管理员”页面上显示登录用户的姓名。

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.

我不知道在这里写什么 'with(['name'=>" "]);'。请帮帮我。

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>

回答by Mahdi Younesi

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

您可以通过以下方式获取当前经过身份验证的用户 Auth::user()

And his name like

而他的名字就像

Auth::user()->name;

Auth::user()->name;

回答by Prashant Prajapati

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!!"]);
    }
}

回答by waltz

In my case I used the authentication login command to make the laravel framework do the login system. You are logged in as :

在我的例子中,我使用了 authentication login 命令来让 Laravel 框架做登录系统。您登录为 :

{{ Auth::user()->name }}</div>

{{ Auth::user()->name }}</div>

回答by Anil Kumar Sahu

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

如果您使用 Laravel 默认身份验证模型,您可以简单地使用{{Auth::user()->name}}它将打印登录用户的名称。

回答by Hooman

Try to use this in the controller:

尝试在控制器中使用它:

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

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

然后在视图中,您需要创建一个 foreach 循环:

@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:

此外,如果您使用 laravel 的默认身份验证,则可以使用它:

{{ Auth::user()->name }}

回答by Alexey Mezenin

You've said Auth::user()->namedoesn'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, @authand other directives will not work for you.

你说过Auth::user()->name这对你不起作用,那是你保持密码不变,而不是散列它。此外,看起来您正在使用多个表来存储用户。在这种情况下,Auth::check()@guest@auth和其他指令将不会为你工作。

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:

您需要添加一个新的新守卫,修改一些 Laravel 代码并手动登录用户。但是由于您是 Laravel 的新手,我认为对您来说更简单的解决方案是手动检查用户:

$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:

并执行此操作而不是@auth

if (session('user'))

And this instead of @guest:

而这而不是@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()帮助程序:

{{ 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.

而且我真的建议您使用brcypt()helper对密码进行哈希处理,并为所有用户使用一张表,并只为用户分配角色。在这种情况下,您将能够使用内置的身份验证系统,并且用户的凭据将是安全的。