Laravel: Auth::user()-id trying to get a property of a non-object
If you are using Sentry
check the logged in user with Sentry::getUser()->id
. The error you get is that the Auth::user()
returns NULL and it tries to get id from NULL hence the error trying to get a property from a non-object
.
Laravel: Trying to get property 'id' of non-object for Auth::User()-id;
here is a solution
you can find that user in db with email and password you have and get its id
and then use LoginUsingId($id) to login your user
Trying to get property 'id' of non-object Laravel - Auth::id() command
Case closed. Thank you guys for you responses! I managed to fix my problem.
I found solution here: ANSWER
But I added stack directly to middleware, not in the 'web' group.
This is my /Http/kernel.php file:
protected $middleware = [
\Illuminate\Session\Middleware\StartSession::class,
\App\Http\Middleware\TrustProxies::class,
\App\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class
];
Trying to get property 'id' of non-object Auth::user()-id in Controller Laravel?
Auth::user()
and Auth::check()
will return null
/false
(respectively) in two situations: if you're not logged in at all (for obvious reasons) or if your route doesn't have sessions or API token authentication enabled.
In a default install, the web
routes get session-based authentication, and the api
routes get token-based authentication. Ensuring the route has the proper set of middleware (or custom middleware, if you're rolling your own authentication criteria) does the trick.
Trying to get property of non-object - Laravel 5
Is your query returning array or object? If you dump it out, you might find that it's an array and all you need is an array access ([]) instead of an object access (->).
Trying to get property 'name' of non-object laravel 8
That's because when a user is not logged in, Auth::user() will be null and you cannot access a property name on null.
You can tackle this in
@auth
<span class="mr-2 d-none d-lg-inline text-gray-600 small">
{{ Auth::user()->name }}
</span>
@endauth
//OR
<span class="mr-2 d-none d-lg-inline text-gray-600 small">
{{ optional(Auth::user())->name }}
</span>
Out of the above two options using @auth is better because in the second option an empty span will still be included in the page. Where as with @auth whatever is in between @auth and @endauth will only be included on the page if user is logged in.
Related Topics
Call ASP.NET Web Service from PHP with Multiple Parameters
Problem with Adding Root Path Using PHP Domdocument
Multiple Auto Increment in MySQL
Concatenate PHP Function Output to a String Like Variables
Checking for Empty Arrays: Count VS Empty
Storage in Laravel Says Symlink - No Such File
Read in Text File Line by Line PHP - Newline Not Being Detected
PHP Method="Post" Stopped Working After I Added This .Htaccess... Why
Fatal Error: Cannot Use Object of Type Stdclass as Array In
What Rss Parser Should I Use in PHP
Minimum Cart Amount for Specific Product Categories in Woocommerce
Get Return Value from SQL Stored Procedure Using PHP
PHP How to Go One Level Up on Dirname(_File_)
How to Retrieve Youtube Video Details from Video Url Using PHP