I\'m getting the following error \"trying to get a property of a non-object\" when I submit a form to add a user, the error is apparently on the first line: Auth::user()->id
Never forget to include and try to use middleware auth:
use Illuminate\Http\Request;
Then find the id using request method:
$id= $request->user()->id;
The first check user logged in and then
if (Auth::check()){
//get id of logged in user
{{ Auth::getUser()->id}}
//get the name of logged in user
{{ Auth::getUser()->name }}
}
Do a Auth::check() before to be sure that you are well logged in :
if (Auth::check())
{
// The user is logged in...
}
In Laravel 8.6, the following works for me in a controller:
$id = auth()->user()->id;
For Laravel 6.X you can do the following:
$user = Auth::guard(<GUARD_NAME>)->user();
$user_id = $user->id;
$full_name = $user->full_name;
if(Auth::check() && Auth::user()->role->id == 2){
$tags = Tag::latest()->get();
return view('admin.tag.index',compact('tags'));
}