Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

thinkjay's avatar

Trying to get property of non-object

Hi what am I missing here, this was working a few days ago and not sure what I did so that it's now giving me this error at the line where it says "return $userEmail;"

ErrorException in routes.php 
Trying to get property of non-object

Here's my routes.php file:

use App\Question;
use App\User;
use Illuminate\Support\Facades\Auth;

Route::get('/', function()
{
    $user = Auth::User();
    $userEmail = Auth::User()->email;
    return $userEmail;
});
0 likes
51 replies
mstnorris's avatar

It's

Route::get('/', function()
{
    $user = Auth::user(); // lowercase
    $userEmail = $user->email; // lowercase and tidied up, you don't need to keep referring to Auth::user once you've done it
    return $userEmail;
});

Also, I don't know if you need the use statements.

thinkjay's avatar

@mstnorris Yes, I put this code before the code above and it verifies i'm logged in

if (Auth::check()) { 'You are logged in'; }

mstnorris's avatar

What you said just then doesn't make sense:

if (Auth::check()) { 'You are logged in'; }

It should be:

if (Auth::check()) {
 echo 'You are logged in'; // you currently don't have an echo or var_dump statement
}

Instead of

use Illuminate\Support\Facades\Auth;

try

use Auth;

Also tell me what you get when you dd(Auth::user()

thinkjay's avatar

@mstnorris

I get this error now:

ErrorException in routes.php line 4: The use statement with non-compound name 'Auth' has no effect

mstnorris's avatar

Can you show me your full routes file within ``` ... ``` so I can check it.

thinkjay's avatar

@mstnorris ok i stripped everything - i'm still getting the same error

<?php
use App\User;
use Illuminate\Support\Facades\Auth;

/*
|--------------------------------------------------------------------------
| Welcome Controller
|--------------------------------------------------------------------------
|
*/

Route::get('/', function()
{

    if (Auth::check())
    {
        $user = Auth::user();
        $userEmail = Auth::user()->email;
        return $userEmail;
    }

    else {
        $user = Auth::user();
        $userEmail = Auth::user()->email;
        return $userEmail;
    }
});





Route::controllers([
    'auth' => 'Auth\AuthController',
    'password' => 'Auth\PasswordController',
]);

mstnorris's avatar

You're not using Auth::check() correctly.

Route::get('/', function()
{

    if (Auth::check())
    {
        $user = Auth::user(); // this is necessary as you don't use the variable $user
        $userEmail = Auth::user()->email;
        return $userEmail;
    }

// the else makes no sense as you wouldn't have an authenticated user.
    else {
        $user = Auth::user();
        $userEmail = Auth::user()->email;
        return $userEmail;
    }
});

It should be something like

Route::get('/', function()
{

    if (Auth::check())
    {
        $user = Auth::user(); // this is necessary as you don't use the variable $user
        $userEmail = Auth::user()->email;
        return 'you must log in';
    }
});

Also, why are you assigning $user = Auth::user(); when you don't use that variable?

thinkjay's avatar

Sorry the code was setup properly before I pasted here, I tried to strip it down just to see why it's throwing the "Trying to get property of non-object" error.

I used the Auth check in your second code block but it's still giving me the error. I don't have anything else, here's the entire routes.php file

<?php
use App\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Welcome Controller
|--------------------------------------------------------------------------
|
*/

Route::get('/', function()
{

    if (Auth::check())
    {
        $user = Auth::user(); // this is necessary as you don't use the variable $user
        $userEmail = Auth::user()->email;
        return 'you must log in';
    }


});

 


Route::controllers([
    'auth' => 'Auth\AuthController',
    'password' => 'Auth\PasswordController',
]);

Here is my Model User.php

<?php namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract {

    use Authenticatable, CanResetPassword;

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name', 'email', 'password'];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = ['password', 'remember_token'];

    // A user can have many answers
    public function answers()
    {
        return $this->hasMany('App\Question');
    }


}
mstnorris's avatar

What version of Laravel are you using?

I'm using Laravel 5 and in my routes.php file there are no use statements at the top.

mstnorris's avatar

Also, I don't think you understand what Auth::check() does.

Route::get('/', function()
{
    if (Auth::check())
    {
        // the user IS authenticated therefore we should have access to their email address (provided that is what the column is called)
        $user = Auth::user(); // this is necessary as you don't use the variable $user
        $userEmail = Auth::user()->email;
        return $userEmail; 
    } else {
        // the user is NOT authenticated therefore we return with a simple message
        return 'you must log in'; 
    }
});
  1. Does it give you any other information such as line numbers when you get the error:
ErrorException in routes.php 
Trying to get property of non-object
  1. Can you show me your create_users_table migration file.
thinkjay's avatar

I'm using Laravel 5. I do know what it does, this code was originally displayed at the top right of the page, if user is logged in - display their email. It was working fine but for some reason I can't get the values from the User model. I'm simply just trying to retrieve the values of the logged in user at this point. I just don't know where it went wrong.

mstnorris's avatar

Post your full routes.php exactly as it is now and then tell me what URL you are hitting.

thinkjay's avatar

Yeah sure, thanks for your help again

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password', 60);
            $table->tinyInteger('roleType');
            $table->rememberToken();
            $table->timestamps();

        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('users');
    }

}
thinkjay's avatar

URL: http://localhost:8000/ routes.php

<?php
use App\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;



Route::get('/', function()
{

    if (Auth::check())
    {
        $userEmail = Auth::user()->email;
        return 'success - you ARE logged in';
    }

    return 'NOT logged in';


});


Route::controllers([
    'auth' => 'Auth\AuthController',
    'password' => 'Auth\PasswordController',
]);

When I log out (http://localhost:8000/auth/logout), it displays "NOT logged IN'. When I log in (http://localhost:8000/auth/login), I get

ErrorException in routes.php line 13:
Trying to get property of non-object

Line 13 is $userEmail = Auth::user()->email;

mstnorris's avatar

Try

Route::get('/', ['middleware' => 'auth', function()
{

    if (Auth::check())
    {
        $userEmail = Auth::user()->email;
        return 'success - you ARE logged in';
    }

    return 'NOT logged in';


}]);
mstnorris's avatar

Are you sure you are logged in?

Can you fake it using

Auth::loginUsingId(1); // place this before your Route::get( ...
thinkjay's avatar

Yeah tried both of your suggestions - still no luck

use App\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;


Auth::loginUsingId(1);
Route::get('/', function()
{
    $userEmail = Auth::user()->email;
    return $userEmail;
});




Route::controllers([
    'auth' => 'Auth\AuthController',
    'password' => 'Auth\PasswordController',
]);
mstnorris's avatar

Then there is something else going on as your code there is fine.

Jonathan's avatar

See what Auth::user() is returning:

Route::get('/', function()
{

    if (Auth::check())
    {
        dd(Auth::user());
        $userEmail = Auth::user()->email;
        return 'success - you ARE logged in';
    }

    return 'NOT logged in';


});

Also, you do not need the use statements at the top for Auth and Route.

mstnorris's avatar

Are you able to access the /login route?

If not, then are you trying to get the $user->userEmail from elsewhere in your app, like in a navbar or something

thinkjay's avatar

I tried the code above and It's returning "null" I have it in my master.blade file but that isn't being referenced at all in the routes, I deleted the code in that file and still same error.

I did a find in my project folder, it's only displayed here /storage/framework/views/124ff8483b2b3ab50a0ee0a062e83049 /storage/framework/views/cb4bbb7914d01398bdb8279db5c0ceba

mstnorris's avatar

try and run the following in the command line

composer dump-autoload -o and php artisan clear-compiled

And reset your browsers cache.

mstnorris's avatar

@thinkjay the fact that it is returning null (what I suggested earlier was to dd(Auth::user())), if that is what you're saying is returning null, then no one is logged in.

sitesense's avatar

Just wondering, have you tried:

$userEmail = \Auth::user()->email;
thinkjay's avatar

I still can't figure this out...

    $id = Auth::id();
    return $id;

The code above works, it returns the user's ID, i've verified that I'm logged in by redirecting to a URL specified in my RedirectIfAuthenticated.php


But I still am unable to access the user's object such as the email. This code below still returns "Trying to get property of non-object"

$userEmail = Auth::user()->email;
return $userEmail;
Next

Please or to participate in this conversation.