Reputation: 85
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!
Upvotes: 0
Views: 2092
Reputation: 1128
use this code
<h1> Hi there {{ Auth::guard('admin')->user()->username }} you are logged in as Admin
Upvotes: 3
Reputation: 2453
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
Upvotes: 0