gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 02:13:33 27 4
gpt4 key购买 nike

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

//文件名: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;

用法:

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

完全错误
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( )

编辑:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Setting extends Model {

}

最佳答案

运行时 测试用例 Laravel 6.x 及以上 ,我经常遇到这个错误。我意识到单元测试是从 PHPUnit TestClass 而不是 Laravel TestCase 扩展的。所以改PHPUnit\Framework\TestCase;Tests\TestCase .

<?php


use Tests\TestCase;

class ArtifactTest extends TestCase{

}

关于laravel - 在 Laravel 5 上的非对象错误上调用成员函数 connection(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31102570/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com