我一直在 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
我该如何解决?
到目前为止,我已经发现,在这个链接中,我需要创建一个新的应用程序容器,然后将它绑定到 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();