gpt4 book ai didi

Eloquent 错误: A facade root has not been set

转载 作者:行者123 更新时间:2023-12-03 08:11:34 52 4
gpt4 key购买 nike

我一直在 Slim Framework 2 中成功地使用 Eloquent 作为独立包。

但是现在我想使用 Illuminate\Support\Facades\DB 因为我需要通过从 2 个表中获取信息并使用数据库中的左连接和计数器来显示一些统计信息,如下所示:

use Illuminate\Support\Facades\DB;
$projectsbyarea = DB::table('projects AS p')
->select(DB::raw('DISTINCT a.area, COUNT(a.area) AS Quantity'))
->leftJoin('areas AS a','p.area_id','=','a.id')
->where('p.status','in_process')
->where('a.area','<>','NULL')
->orderBy('p.area_id');

我收到以下错误:
Type: RuntimeException
Message: A facade root has not been set.
File: ...\vendor\illuminate\support\Facades\Facade.php
Line: 206

我该如何解决?

到目前为止,我在 this link 中发现了我需要创建一个新的应用程序容器,然后将其绑定(bind)到 Facade。但我还没有找到如何使它工作。

这就是我如何开始我的 Eloquent 的其余部分并且工作正常:
use Illuminate\Database\Capsule\Manager as Capsule;

$capsule = new Capsule();

$capsule->addConnection([
'my' => $app->config->get('settings'),
/* more settings ...*/
]);

/*booting Eloquent*/
$capsule->bootEloquent();

我该如何解决?

固定
正如@user5972059 所说,我必须添加 $capsule->setAsGlobal();//This is important to make work the DB (Capsule)正上方 $capsule->bootEloquent();
然后,查询是这样执行的:
use Illuminate\Database\Capsule\Manager as Capsule;
$projectsbyarea = Capsule::table('projects AS p')
->select(DB::raw('DISTINCT a.area, COUNT(a.area) AS Quantity'))
->leftJoin('areas AS a','p.area_id','=','a.id')
->where('p.status','in_process')
->where('a.area','<>','NULL')
->orderBy('p.area_id')
->get();

最佳答案

与 laravel 8 有同样的问题。我替换了

   use PHPUnit\Framework\TestCase;
和:
 use Tests\TestCase;

关于 Eloquent 错误: A facade root has not been set,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35418810/

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