简体   繁体   中英

Laravel AUTH with Guard Show Logged in User credentials

I'm Having Problem with my multi Auth Laravel application. I am trying to login my admin user with a guard which is admin, {{ auth()->user()->username }} this is the code that im using to show the logged in user username it works fine but when i put it on my admin dashboard it gives me error

This is the error i got

"Trying to get property 'name' of non-object (View: C:\\xampp\\htdocs\\Projects\\centralsocialv2.4\\resources\\views\\admindashboard.blade.php)"

This is my 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',
];
}

This is what handles my Admin Login on 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');

}

My Dashboard view where admin redirects if logged in

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

Something wrong with my Code? Thank you guys!

使用此代码

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

As your column name is username change Auth::user()->name to Auth::user()->username

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

Or you can also use auth()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM