Eloquent error: A facade root has not been set

孤者浪人 提交于 2019-11-26 20:07:30

问题


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

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');

I get the following error:

Type: RuntimeException
Message: A facade root has not been set.
File: ...\vendor\illuminate\support\Facades\Facade.php
Line: 206

How can I solve it?

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();

How do I fix this?

Fixed As @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 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();

回答1:


You have to change your code to:

$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;



回答2:


I have just solved this problem by uncommenting $app->withFacades(); in bootstrap/app.php




回答3:


Try uncommenting in app.php $app->withFacades();




回答4:


Why someone marked answer not useful it perfectly worked for me. i was using use Illuminate\Support\Facades\DB as DB; but this worked use Illuminate\Database\Capsule\Manager as DB; after setting capsule as global $capsule->setAsGlobal(); in slim.




回答5:


If you recently upgrade Laravel on Homestead & VirtualBox environment or do not find any reason that causing please be sure your Vagrant is up to date.

Referance

I had Taylor lock this thread. The past several replies have restated the solution, which is to Upgrade to Virtualbox 6.x, the thread is locked to prevent other issues that are not related from being dogpiled on here.




回答6:


wrong way

public function register()
   {
       $this->app->bind('Activity', function($app)
       {
            new Activity;
       });
   }

right way 👍

public function register()
   {
       $this->app->bind('Activity', function($app)
       {
            return new Activity;
       });
   }

---------------------------------- don't forget return



来源:https://stackoverflow.com/questions/35418810/eloquent-error-a-facade-root-has-not-been-set

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!