Eloquent error: A facade root has not been set
Eloquent error: A facade root has not been set
我已经在 Slim Framework 2 中成功地使用 Eloquent 作为独立包。
但现在我想使用 Illuminate\Support\Facades\DB,因为我需要通过从 2 个表中获取信息并使用 Left Join 和数据库中的计数器来显示一些统计信息,如下所示:
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
我该如何解决?
到目前为止,我发现,在 this link 中,我需要创建一个新的应用程序容器,然后将其绑定到 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();
我该如何解决这个问题?
固定
正如@user5972059 所说,我必须在 $capsule->bootEloquent();
上方添加 $capsule->setAsGlobal();//This is important to make work the DB (Capsule)
然后,查询是这样执行的:
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();
并且在 class 文件的开头,您必须导入:
use Illuminate\Database\Capsule\Manager as DB;
我刚刚通过取消注释 bootstrap/app 中的 $app->withFacades();
解决了这个问题。php
尝试取消注释 app.php $app->withFacades();
如果您最近在 Homestead 和 VirtualBox 环境上升级 Laravel 或者没有找到任何导致的原因,请确保您的 Vagrant 是最新的。
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.
错误的方式
public function register()
{
$this->app->bind('Activity', function($app)
{
new Activity;
});
}
正确的方式
public function register()
{
$this->app->bind('Activity', function($app)
{
return new Activity;
});
}
--------------------------------别忘了return
我在 运行 之后收到此错误:
$ php artisan config:cache
我的解决方案是删除 /bootstrap/cache/config.php
文件。我是 运行 Laravel 5.5.
似乎出现在多种情况下,而不仅仅是外墙。
在我的项目中,我在实例化对象时通过使用 Laravel 依赖注入设法解决了这个问题。以前我是这样的:
$class = new MyClass(
new Client(),
env('client_id', 'test'),
Config::get('myapp.client_secret')
);
当我使用 Laravel env() 和 Config() 时出现相同的错误消息。
我在AppServiceProvider中引入Client和env是这样的:
$this->app->bind(
MyClass::class,
function () {
return new MyClass(
new Client(),
env('client_id', 'test')),
Config::get('myapp.client_secret')
);
}
然后像这样实例化 class:
$class = app(MyClass::class);
查看 https://laravel.com/docs/5.8/container 的更多内容。
在我的例子中,所有门面都不可用,包括 artisan 的任何命令。
删除vendor后重新安装,问题依旧
终于找到问题了,因为我用的是bensampo/laravel-enum
库,在config文件中使用了类似下面的语句,
'name' =>\App\Enums\VipLevelEnum::getDescription(
\App\Enums\VipLevelEnum::GOLD
)
由于尚未加载 Lang facade(我猜),这可能会导致错误
你的错误可能无处不在。当您在 Apache 服务器上使用 laravel 时,您可能看不到错误的详细信息。
你能试试吗
php artisan serve --port=8080
更详细的错误。
使用 phpUnit 测试 laravel 的一个随机问题是 laravel 门面在测试时尚未初始化。
而不是使用标准的 PHPUnit TestCase class
class MyTestClass extends PHPUnit\Framework\TestCase
一个可以用
class UserTest extends Illuminate\Foundation\Testing\TestCase
这个问题就解决了。
与 laravel 8 有同样的问题。我替换了
use PHPUnit\Framework\TestCase;
与:
use Tests\TestCase;
我在 运行 使用 PHPUnit v.9.5.4、PHP v.8.0.3 和 Lumen v.8.2.2 进行测试时收到以下消息:
PHP Fatal error: Uncaught RuntimeException: A facade root has not
been set. in path_to_project/vendor/illuminate/support/Facades/Facade.php:258
虽然我 显然 已经配置了我的 app.php 以启用外墙 ($app->withFacades();
),但我仍然在尝试时收到此错误消息运行 测试使用 Illuminate\Support\Facades\DB
。不幸的是,none 的其他答案对我有所帮助。
这个错误实际上是由于我在 phpunit.xml
中的 配置而引发的,它没有指向我的 app.php 文件,我实际启用外墙的地方。
我只需要改变
<phpunit (...OTHER_PARAMS_HERE) bootstrap="vendor/autoload.php">
到
<phpunit (...OTHER_PARAMS_HERE) bootstrap="bootstrap/app.php">
希望对您有所帮助。
为php升级版本,我在调用接口时遇到了这个错误。
$ php artisan config:cache
删除/bootstrap/cache/config.php文件是一个非常有效的方法。
就我而言,有一段时间 运行 在 PHP 版本 8 中有一个 PHP 项目,那时我使用了一些 PHP 8 功能,例如参数定义和方法的多个 return 类型声明仅受 PHP 8 及更高版本支持。当我从 PHP 8 降级到 PHP 7.4 时,我遇到了这个问题。删除 return 类型和参数提示后,问题就消失了。
别忘了打电话给parent::setUp();
。
失败
public function setUp(): void {
Config::set('something', true);
}
有效
public function setUp(): void {
parent::setUp();
Config::set('something', true);
}
测试于 Laravel 8.78
tests/bootstrap.php
<?php
use Illuminate\Foundation\Bootstrap\RegisterFacades;
use Illuminate\Foundation\Bootstrap\LoadConfiguration;
require_once __DIR__ . '/../vendor/autoload.php';
$app = require_once __DIR__ . '/../bootstrap/app.php';
(new LoadConfiguration())->bootstrap($app);// <------- Required for next line
(new RegisterFacades())->bootstrap($app);// <------- Add this line
我已经在 Slim Framework 2 中成功地使用 Eloquent 作为独立包。
但现在我想使用 Illuminate\Support\Facades\DB,因为我需要通过从 2 个表中获取信息并使用 Left Join 和数据库中的计数器来显示一些统计信息,如下所示:
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
我该如何解决?
到目前为止,我发现,在 this link 中,我需要创建一个新的应用程序容器,然后将其绑定到 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();
我该如何解决这个问题?
固定
正如@user5972059 所说,我必须在 $capsule->bootEloquent();
$capsule->setAsGlobal();//This is important to make work the DB (Capsule)
然后,查询是这样执行的:
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();
并且在 class 文件的开头,您必须导入:
use Illuminate\Database\Capsule\Manager as DB;
我刚刚通过取消注释 bootstrap/app 中的 $app->withFacades();
解决了这个问题。php
尝试取消注释 app.php $app->withFacades();
如果您最近在 Homestead 和 VirtualBox 环境上升级 Laravel 或者没有找到任何导致的原因,请确保您的 Vagrant 是最新的。
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.
错误的方式
public function register()
{
$this->app->bind('Activity', function($app)
{
new Activity;
});
}
正确的方式
public function register()
{
$this->app->bind('Activity', function($app)
{
return new Activity;
});
}
--------------------------------别忘了return
我在 运行 之后收到此错误:
$ php artisan config:cache
我的解决方案是删除 /bootstrap/cache/config.php
文件。我是 运行 Laravel 5.5.
似乎出现在多种情况下,而不仅仅是外墙。
在我的项目中,我在实例化对象时通过使用 Laravel 依赖注入设法解决了这个问题。以前我是这样的:
$class = new MyClass(
new Client(),
env('client_id', 'test'),
Config::get('myapp.client_secret')
);
当我使用 Laravel env() 和 Config() 时出现相同的错误消息。
我在AppServiceProvider中引入Client和env是这样的:
$this->app->bind(
MyClass::class,
function () {
return new MyClass(
new Client(),
env('client_id', 'test')),
Config::get('myapp.client_secret')
);
}
然后像这样实例化 class:
$class = app(MyClass::class);
查看 https://laravel.com/docs/5.8/container 的更多内容。
在我的例子中,所有门面都不可用,包括 artisan 的任何命令。
删除vendor后重新安装,问题依旧
终于找到问题了,因为我用的是bensampo/laravel-enum
库,在config文件中使用了类似下面的语句,
'name' =>\App\Enums\VipLevelEnum::getDescription(
\App\Enums\VipLevelEnum::GOLD
)
由于尚未加载 Lang facade(我猜),这可能会导致错误
你的错误可能无处不在。当您在 Apache 服务器上使用 laravel 时,您可能看不到错误的详细信息。
你能试试吗
php artisan serve --port=8080
更详细的错误。
使用 phpUnit 测试 laravel 的一个随机问题是 laravel 门面在测试时尚未初始化。
而不是使用标准的 PHPUnit TestCase class
class MyTestClass extends PHPUnit\Framework\TestCase
一个可以用
class UserTest extends Illuminate\Foundation\Testing\TestCase
这个问题就解决了。
与 laravel 8 有同样的问题。我替换了
use PHPUnit\Framework\TestCase;
与:
use Tests\TestCase;
我在 运行 使用 PHPUnit v.9.5.4、PHP v.8.0.3 和 Lumen v.8.2.2 进行测试时收到以下消息:
PHP Fatal error: Uncaught RuntimeException: A facade root has not been set. in path_to_project/vendor/illuminate/support/Facades/Facade.php:258
虽然我 显然 已经配置了我的 app.php 以启用外墙 ($app->withFacades();
),但我仍然在尝试时收到此错误消息运行 测试使用 Illuminate\Support\Facades\DB
。不幸的是,none 的其他答案对我有所帮助。
这个错误实际上是由于我在 phpunit.xml
中的 配置而引发的,它没有指向我的 app.php 文件,我实际启用外墙的地方。
我只需要改变
<phpunit (...OTHER_PARAMS_HERE) bootstrap="vendor/autoload.php">
到
<phpunit (...OTHER_PARAMS_HERE) bootstrap="bootstrap/app.php">
希望对您有所帮助。
为php升级版本,我在调用接口时遇到了这个错误。
$ php artisan config:cache
删除/bootstrap/cache/config.php文件是一个非常有效的方法。
就我而言,有一段时间 运行 在 PHP 版本 8 中有一个 PHP 项目,那时我使用了一些 PHP 8 功能,例如参数定义和方法的多个 return 类型声明仅受 PHP 8 及更高版本支持。当我从 PHP 8 降级到 PHP 7.4 时,我遇到了这个问题。删除 return 类型和参数提示后,问题就消失了。
别忘了打电话给parent::setUp();
。
失败
public function setUp(): void {
Config::set('something', true);
}
有效
public function setUp(): void {
parent::setUp();
Config::set('something', true);
}
测试于 Laravel 8.78
tests/bootstrap.php
<?php
use Illuminate\Foundation\Bootstrap\RegisterFacades;
use Illuminate\Foundation\Bootstrap\LoadConfiguration;
require_once __DIR__ . '/../vendor/autoload.php';
$app = require_once __DIR__ . '/../bootstrap/app.php';
(new LoadConfiguration())->bootstrap($app);// <------- Required for next line
(new RegisterFacades())->bootstrap($app);// <------- Add this line