Eloquent error: A facade root has not been set

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

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;

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


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