Laravel AUTH与Guard Show登录用户凭证

时间:2018-11-29 11:53:00

标签: php laravel

我的多身份验证Laravel应用程序出现问题。我正在尝试使用管理员admin作为管理员登录我的管理员用户,{{ auth()->user()->username }}这是我用来显示已登录用户名的代码,它可以正常工作,但是当我将其放在管理控制台上时,它给了我错误

这是我得到的错误

    “试图获取非对象的属性'名称'(视图:C:\ xampp \ htdocs \ Projects \ centralsocialv2.4 \ resources \ views \ admindashboard.blade.php)”

这是我的Admin.php

class Admin extends Authenticatable
{
    use Notifiable;

    protected $guard = 'admin';

    protected $table = 'admins';


    protected $fillable = [
      'username', 'password',
];

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'password', 'remember_token',
];
}

这是处理我在AdminController.php上的管理员登录信息的原因

public function adminlogin(Request $request)
{

       // Validation 
       $this->validate($request, [
        'username' => 'required',
        'password' => 'required'
  ]);



  $username = $request->input('username');
  $password = $request->input('password');

  if (Auth::guard('admin')->attempt(['username' => $username, 'password' => $password]))
  {
   return view('admindashboard');;
  }
   return ('Nope');

}

我的仪表板视图,如果管理员登录后将重定向到其中

<h1> Hi there   {{ Auth::user()->name }}   you are logged in as Admin 
</h1> 

@if (!Auth::guard('admin')->check())
no admin
@else
yes admin
@endif

<a href="{{ route('userlogout') }}" > Log out </a>

我的代码有问题吗?谢谢你们!

2 个答案:

答案 0 :(得分:3)

使用此代码

<h1> Hi there   {{ Auth::guard('admin')->user()->username  }}   you are logged in as Admin 

答案 1 :(得分:0)

当您的列名是Assignment时,请将public IActionResult Assignment(Guid id, [FromBody] AssignmentModel thisNameDoesNotMatter) 更改为username

Auth::user()->name

或者您也可以使用Auth::user()->username

<h1> Hi there   {{ Auth::user()->username }}   you are logged in as Admin