Laravel: Auth::User()->Id Trying to Get a Property of a Non-Object

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



Leave a reply



Submit