雄辩的错误:尚未设置外观根 [英] Eloquent error: A facade root has not been set
问题描述
我已经成功地将Eloquent用作Slim Framework 2中的独立软件包。
I have been using Eloquent as a standalone package in Slim Framework 2 successfully.
但是现在我想使用Illuminate\Support\Facades\ \DB,因为我需要显示一些统计信息,方法是从2个表中获取信息,并从数据库中使用左连接和计数器,如下所示:
But now that I want to make use of Illuminate\Support\Facades\DB since I need to show some statistics by getting the info from 2 tables and using a Left Join and a Counter from the database like this:
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。
So far I have found out, in this link that I need to create a new app container and then bind it to the Facade. But I haven't found out how to make it work.
这是我开始其余的雄辩和正常工作的方式:
This is how I started the rest of my Eloquent and working fine:
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(); //这对于使数据库(胶囊)有效很重要
刚好在 $ capsule-> bootEloquent();
之后,查询为执行如下:
Then, the query is executed like this:
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();
推荐答案
您必须将代码更改为:
$Capsule = new Capsule;
$Capsule->addConnection(config::get('database'));
$Capsule->setAsGlobal(); //this is important
$Capsule->bootEloquent();
在班级文件的开头,您必须导入:
And at the beginning of your class file you have to import:
use Illuminate\Database\Capsule\Manager as DB;
这篇关于雄辩的错误:尚未设置外观根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!