php artisan route :: list给我一个错误:试图获取非对象的属性

时间:2015-12-26 04:56:28

标签: php laravel

当我输入

php artisan route:list

我得到了

 Trying to get property of non-object

当我输入

php artisan route:list -v

我得到了

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

以下是相关代码

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

错误消息似乎告诉我有关$ user的错误 事情是当我dd $ categorySetting或$ user时,它给了我正确的对象......

    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
}

知道什么是错的???

1 个答案:

答案 0 :(得分:3)

Auth :: user()仅在用户登录时返回User模型,因此当您在浏览器上进行操作时,将返回您的用户。

但是,当您从cli调用route:list时,$ user为null,因此“尝试获取非对象的属性”。

你需要弄清楚路由:list调用getDefaultSettings()的原因。我的猜测是你在routes.php文件上做了一些调用该函数的东西。

解决问题的一种快速方法是使用check方法检查是否有用户在使用用户模型执行任何其他操作之前登录:

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