本文介绍了Eloquent 错误:尚未设置外观根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Slim Framework 2 中成功地将 Eloquent 作为独立包使用.

I have been using Eloquent as a standalone package in Slim Framework 2 successfully.

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

But now that I want to make use of IlluminateSupportFacadesDB 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 IlluminateSupportFacadesDB;
$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: ...vendorilluminatesupportFacadesFacade.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.

这就是我如何开始我的 Eloquent 的其余部分并且工作正常:

This is how I started the rest of my Eloquent and working fine:

use IlluminateDatabaseCapsuleManager as Capsule;

$capsule = new Capsule();

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

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

我该如何解决这个问题?

How do I fix this?

已修复正如@user5972059 所说,我必须添加 $capsule->setAsGlobal();//这对于在 $capsule->bootEloquent();

FixedAs @user5972059 said, I had to add $capsule->setAsGlobal();//This is important to make work the DB (Capsule) just above $capsule->bootEloquent();

然后,查询是这样执行的:

Then, the query is executed like this:

use IlluminateDatabaseCapsuleManager 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 IlluminateDatabaseCapsuleManager as DB;

这篇关于Eloquent 错误:尚未设置外观根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 03:53