在 Laravel 5 上的非对象错误上调用成员函数 connection()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31102570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:51:14  来源:igfitidea点击:

Call to a member function connection() on a non-object error on Laravel 5

laraveleloquentlaravel-5

提问by user3407278

I want to store user configuration data on database and I'm following this forum thread about it http://forumsarchive.laravel.io/viewtopic.php?id=10406but when I implemented it laravel throws an error. I ran composer dump-autoload but nothing seems to be workign.What's the problem here?

我想将用户配置数据存储在数据库上,我正在关注这个关于它的论坛帖子http://forumsarchive.laravel.io/viewtopic.php?id=10406但是当我实现它时,laravel 抛出一个错误。我运行 composer dump-autoload 但似乎没有任何工作。这里有什么问题?

// filename: app/config/settings.php

// 文件名:app/config/settings.php

use \App\Models\Setting

$list = array();

$format = function(&$list, $keys, $val) use(&$format) {
    $keys ? $format($list[array_shift($keys)], $keys, $val) : $list = $val;
};

foreach(Setting::all() as $setting) {
    $format($list, explode('.', $setting->token), $setting->content);
}

return $list;

Usage:

用法:

echo Config::get('settings.token'); // returns value of 'content'

echo Config::get('settings.token'); // 返回“内容”的值

Full error

完全错误

Fatal error: Call to a member function connection() on a non-object in C:\wamp\www\iapp\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php on line 3137
Call Stack
#   Time    Memory  Function    Location
1   0.0017  247544  {main}( )   ..\index.php:0
2   0.0910  2848480 Illuminate\Foundation\Http\Kernel->handle( )    ..\index.php:58
3   0.0910  2848736 Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter( )  ..\Kernel.php:86
4   0.0917  2886304 Illuminate\Foundation\Http\Kernel->bootstrap( ) ..\Kernel.php:110
5   0.0917  2886472 Illuminate\Foundation\Application->bootstrapWith( ) ..\Kernel.php:215
6   0.0974  2994336 Illuminate\Foundation\Bootstrap\LoadConfiguration->bootstrap( ) ..\Application.php:194
7   0.0986  3025160 Illuminate\Foundation\Bootstrap\LoadConfiguration->loadConfigurationFiles( )    ..\LoadConfiguration.php:38
8   0.1407  3814624 require( 'C:\wamp\www\iapp\config\settings.php' )   ..\LoadConfiguration.php:56
9   0.1407  3814696 ConfigSetting::getSettings( )   ..\settings.php:28
10  0.1494  4488840 Illuminate\Database\Eloquent\Model::all( )  ..\settings.php:16
11  0.1496  4494520 Illuminate\Database\Eloquent\Model->newQuery( ) ..\Model.php:646
12  0.1496  4494616 Illuminate\Database\Eloquent\Model->newQueryWithoutScopes( )    ..\Model.php:1769
13  0.1496  4494688 Illuminate\Database\Eloquent\Model->newBaseQueryBuilder( )  ..\Model.php:1795
14  0.1496  4494736 Illuminate\Database\Eloquent\Model->getConnection( )    ..\Model.php:1852
15  0.1496  4494784 Illuminate\Database\Eloquent\Model::resolveConnection( )

Edit :

编辑 :

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Setting extends Model {

}

命名空间 App\Models;

使用 Illuminate\Database\Eloquent\Model;

类设置扩展模型{

}

回答by alexanderarda

go to bootstrap/app.php

转到引导程序/app.php

Just uncomment

只需取消注释

$app->withEloquent();

回答by Maxim Lanin

Working with models in your settings files is not the best way to use them.

在设置文件中使用模型并不是使用它们的最佳方式。

Your problem is, that your model query started BEFORE Laravel started it's services. That's why, when you try to make your query, model can't resolve it's connection, because DB service hasn't been initiated.

您的问题是,您的模型查询在 Laravel 启动它的服务之前就启动了。这就是为什么当您尝试进行查询时,模型无法解析它的连接,因为数据库服务尚未启动。

If you want do this stuff, create your own ServiceProvider and update your config there, or do it right in the bootmethod of your existing AppServiceProvider.

如果你想做这些事情,创建你自己的 ServiceProvider 并在那里更新你的配置,或者在boot你现有的AppServiceProvider.

回答by Bedram Tamang

While running TestCasein Laravel 6.x and above, I often encounter this error. I realized that the unit test is extended from PHPUnit TestClass rather than Laravel TestCase. So change PHPUnit\Framework\TestCase;to Tests\TestCase.

Laravel 6.x 及更高版本中运行TestCase时,我经常遇到此错误。我意识到单元测试是从 PHPUnit TestClass 而不是 Laravel TestCase 扩展而来的。所以改为.PHPUnit\Framework\TestCase;Tests\TestCase

<?php


use Tests\TestCase;

class ArtifactTest extends TestCase{

}