我已经在 Slim Framework 2 中成功地使用 Eloquent 作为独立包。
但现在我想利用 Illuminate\Support\Facades\DB 因为我需要通过从 2 个表获取信息并使用数据库中的 Left Join 和 Counter 来显示一些统计信息,如下所示:
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
我该如何解决?
到目前为止我发现,在这个链接 https://github.com/nicolaslopezj/searchable/issues/86我需要创建一个新的应用程序容器,然后将其绑定到 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();
我该如何解决?
Fixed正如@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();