php artisan route::list gives me an error: Trying to get property of non-object

- 1 answer

Ad

When I type

php artisan route:list

I get

 Trying to get property of non-object

When I type

php artisan route:list -v

I get

Exception trace:
 () at /home/vagrant/Code/app/CategorySettings.php:31
 Illuminate\Foundation\Bootstrap\HandleExceptions->handleError() at /home/vagrant/Code/app/CategorySettings.php:31
 App\CategorySettings->getDefaultSettings() at /home/vagrant/Code/app/Http/Controllers/CategoryController.php:27
 App\Http\Controllers\CategoryController->__construct() at n/a:n/a
 ReflectionClass->newInstanceArgs() at /home/vagrant/Code/vendor/laravel/framework/src/Illuminate/Container/Container.php:776
 Illuminate\Container\Container->build() at /home/vagrant/Code/vendor/laravel/framework/src/Illuminate/Container/Container.php:627
 Illuminate\Container\Container->make() at /home/vagrant/Code/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:674
 Illuminate\Foundation\Application->make() at /home/vagrant/Code/vendor/laravel/framework/src/Illuminate/Foundation/Console/RouteListCommand.php:170
 Illuminate\Foundation\Console\RouteListCommand->getControllerMiddleware() at /home/vagrant/Code/vendor/laravel/framework/src/Illuminate/Foundation/Console/RouteListCommand.php:151
 Illuminate\Foundation\Console\RouteListCommand->getMiddleware() at /home/vagrant/Code/vendor/laravel/framework/src/Illuminate/Foundation/Console/RouteListCommand.php:119
 Illuminate\Foundation\Console\RouteListCommand->getRouteInformation() at /home/vagrant/Code/vendor/laravel/framework/src/Illuminate/Foundation/Console/RouteListCommand.php:89
 Illuminate\Foundation\Console\RouteListCommand->getRoutes() at /home/vagrant/Code/vendor/laravel/framework/src/Illuminate/Foundation/Console/RouteListCommand.php:76
 Illuminate\Foundation\Console\RouteListCommand->fire() at n/a:n/a
 call_user_func_array() at /home/vagrant/Code/vendor/laravel/framework/src/Illuminate/Container/Container.php:503
 Illuminate\Container\Container->call() at /home/vagrant/Code/vendor/laravel/framework/src/Illuminate/Console/Command.php:150
 Illuminate\Console\Command->execute() at /home/vagrant/Code/vendor/symfony/console/Command/Command.php:256
 Symfony\Component\Console\Command\Command->run() at /home/vagrant/Code/vendor/laravel/framework/src/Illuminate/Console/Command.php:136
 Illuminate\Console\Command->run() at /home/vagrant/Code/vendor/symfony/console/Application.php:838
 Symfony\Component\Console\Application->doRunCommand() at /home/vagrant/Code/vendor/symfony/console/Application.php:189
 Symfony\Component\Console\Application->doRun() at /home/vagrant/Code/vendor/symfony/console/Application.php:120
 Symfony\Component\Console\Application->run() at /home/vagrant/Code/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:107
 Illuminate\Foundation\Console\Kernel->handle() at /home/vagrant/Code/artisan:35

Here is the related code

public function getDefaultSettings()
{
    $user = Auth::user();
    $categorySetting = $user->settings;   // <- line 31
...
}

Error message seems to tell me something wrong about $user Thing is when I dd $categorySetting or $user, it gives me the correct objects...

    User {#418 ▼
  #table: "users"
  #fillable: array:16 [▶]
  #hidden: array:2 [▶]
  #connection: null
  #primaryKey: "id"
  #perPage: 15
  +incrementing: true
  +timestamps: true
  #attributes: array:20 [▶]
  #original: array:20 [▶]
  #relations: array:1 [▶]
  #visible: []
  #appends: []
  #guarded: array:1 [▶]
  #dates: []
  #dateFormat: null
  #casts: []
  #touches: []
  #observables: []
  #with: []
  #morphClass: null
  +exists: true
  +wasRecentlyCreated: false
}

Any idea what's wrong???

Ad

Answer

Ad

Auth::user() only returns the User model when there's an user logged in, so when you dd on the browser, your user is returned.

However, when you call route:list from the cli, $user is null, hence 'Trying to get property of non-object'.

What you need to figure out is why route:list calls getDefaultSettings(). My guess would be that you're doing something on your routes.php file that calls that function.

One quick way to solve the problem is to check whether there's an user logged in before doing anything else with the user model by using the check method:

public function getDefaultSettings()
{
    if (Auth::check()) {
        $user = Auth::user();
        $categorySetting = $user->settings;
    ...
    }
}
Ad
source: stackoverflow.com
Ad