php artisan route::list gives me an error: Trying to get property of non-object
php artisan route::list gives me an error: Trying to get property of non-object
当我输入
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 有问题
问题是当我添加 $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
}
知道出了什么问题吗???
Auth::user() 仅 returns 有用户登录时的用户模型,因此当您在浏览器上执行 dd 时,将返回您的用户。
但是,当您从 cli 调用 route:list 时,$user 为空,因此 'Trying to get property of non-object'.
您需要弄清楚为什么 route:list 调用 getDefaultSettings()。我的猜测是您正在对调用该函数的 routes.php 文件执行某些操作。
解决该问题的一种快速方法是在使用检查方法对用户模型执行任何其他操作之前检查是否有用户登录:
public function getDefaultSettings()
{
if (Auth::check()) {
$user = Auth::user();
$categorySetting = $user->settings;
...
}
}
当我输入
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 有问题 问题是当我添加 $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
}
知道出了什么问题吗???
Auth::user() 仅 returns 有用户登录时的用户模型,因此当您在浏览器上执行 dd 时,将返回您的用户。
但是,当您从 cli 调用 route:list 时,$user 为空,因此 'Trying to get property of non-object'.
您需要弄清楚为什么 route:list 调用 getDefaultSettings()。我的猜测是您正在对调用该函数的 routes.php 文件执行某些操作。
解决该问题的一种快速方法是在使用检查方法对用户模型执行任何其他操作之前检查是否有用户登录:
public function getDefaultSettings()
{
if (Auth::check()) {
$user = Auth::user();
$categorySetting = $user->settings;
...
}
}