我正在将 Laravel ORM 集成到框架中,但遇到错误“目标类 [db.schema] 不存在。”。我正在使用以下代码:
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Capsule\Manager as DB;
use \Illuminate\Container\Container as Container;
use \Illuminate\Support\Facades\Facade;
/**
* Setup a new app instance container
*
* @var Illuminate\Container\Container
*/
$app = new Container();
$app->singleton('app', 'Illuminate\Container\Container');
/**
* Set \$app as FacadeApplication handler
*/
Facade::setFacadeApplication($app);
这里之前调用错误,以下仅供参考。
$capsule = new DB;
$conn = $capsule->addConnection([
'driver' => 'mysql',
'host' => '127.0.0.1',
'database' => 'yf_testing',
'username' => 'yf_testing',
'password' => 'xxxxxx',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_general_ci',
'prefix' => '',
]);
// Make this Capsule instance available globally via static methods... (optional)
$capsule->setAsGlobal();
// Setup the Eloquent ORM... (optional; unless you've used setEventDispatcher())
$capsule->bootEloquent();
$schema = $capsule->schema('default');
该错误似乎表明 db.schema 需要设置,但我对 Laravel 不太了解,无法确定在哪里执行此操作。
有人可以帮忙吗?
堆栈跟踪如下:
Next Illuminate\Contracts\Container\BindingResolutionException: Target class [db.schema] does not exist. in /usr/local/repodeps/path/to/file/vendor/illuminate/container/Container.php:891
Stack trace:
#0 /usr/local/repodeps/path/to/file/vendor/illuminate/container/Container.php(770): Illuminate\Container\Container->build('db.schema')
#1 /usr/local/repodeps/path/to/file/vendor/illuminate/container/Container.php(706): Illuminate\Container\Container->resolve('db.schema', Array)
#2 /usr/local/repodeps/path/to/file/vendor/illuminate/container/Container.php(1431): Illuminate\Container\Container->make('db.schema')
#3 /usr/local/repodeps/path/to/file/vendor/illuminate/support/Facades/Facade.php(225): Illuminate\Container\Container->offsetGet('db.schema')
#4 /usr/local/repodeps/path/to/file/vendor/illuminate/support/Facades/Facade.php(193): Illuminate\Support\Facades\Facade::resolveFacadeInstance('db.schema')
#5 /usr/local/repodeps/path/to/file/vendor/illuminate/support/Facades/Facade.php(332): Illuminate\Support\Facades\Facade::getFacadeRoot()
#6 /usr/local/apache/htdocs/domain.tld/app/migrations/skipper/database/migrations/2024_09_13_154018_skipper_migrations_Chat_2024091315401851.php(30): Illuminate\Support\Facades\Facade::__callStatic('create', Array)
#7 /usr/local/apache/htdocs/domain.tld/defaults/core/chains/Testing/RunTests/RunTestsAction.class.php(72): SkipperMigrationsChat2024091315401851->up()
#8 /usr/local/apache/htdocs/domain.tld/controller/ExecutionContainer.class.php(514): Testing\RunTests\Action->execute(Object(Request\Console))
#9 /usr/local/apache/htdocs/domain.tld/filter/Execution.class.php(410): ExecutionContainer->runAction()
#10 /usr/local/apache/htdocs/domain.tld/filter/FilterChain.class.php(129): Filter\Execution->execute(Object(Filter\FilterChain), Object(ExecutionContainer))
#11 /usr/local/apache/htdocs/domain.tld/filter/Security.class.php(72): Filter\FilterChain->execute(Object(ExecutionContainer))
#12 /usr/local/apache/htdocs/domain.tld/filter/FilterChain.class.php(129): Filter\Security->execute(Object(Filter\FilterChain), Object(ExecutionContainer))
#13 /usr/local/apache/htdocs/domain.tld/controller/ExecutionContainer.class.php(297): Filter\FilterChain->execute(Object(ExecutionContainer))
#14 /usr/local/apache/htdocs/domain.tld/filter/Dispatch.class.php(44): ExecutionContainer->execute()
#15 /usr/local/apache/htdocs/domain.tld/filter/FilterChain.class.php(129): Filter\Dispatch->execute(Object(Filter\FilterChain), Object(ExecutionContainer))
#16 /usr/local/apache/htdocs/domain.tld/controller/Controller.class.php(241): Filter\FilterChain->execute(Object(ExecutionContainer))
#17 /usr/local/apache/htdocs/domain.tld/public/development/console.php(27): Controller->dispatch()
#18 {main}
thrown in /usr/local/repodeps/path/to/file/vendor/illuminate/container/Container.php on line 891
我尝试了代码的各种组合(看到事情如何变化是相当惊人的)我只是不知道它们的相关性或不足以说明问题是什么。
确保数据库服务提供程序已加载:确保您正在注册数据库服务提供程序。您可以将以下行添加到容器设置中:
$app->register('Illuminate\Database\DatabaseServiceProvider');
设置数据库连接:将连接添加到 Capsule 后,请确保还正确初始化数据库管理器。具体方法如下:
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Support\Facades\Facade;
// Create a new Capsule instance
$capsule = new Capsule;
// Add your connection
$capsule->addConnection([
'driver' => 'mysql',
'host' => '127.0.0.1',
'database' => 'yf_testing',
'username' => 'yf_testing',
'password' => 'xxxxxx',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_general_ci',
'prefix' => '',
]);
// Set the global instance
$capsule->setAsGlobal();
// Boot Eloquent
$capsule->bootEloquent();
// Register the database manager in the container
$app->singleton('db', function($app) use ($capsule) {
return $capsule;
});
注册 Schema Facade:注册数据库服务提供者后,确保 Schema Facade 已正确解析:
$app->singleton('db.schema', function($app) {
return new \Illuminate\Database\Schema\Builder($app['db']->getConnection());
});
调整您的架构使用:通过这些调整,您应该能够使用架构外观而不会遇到绑定解析异常。例如:
Schema::create('your_table', function (Blueprint $table) {
$table->id();
$table->string('name');
// Other columns...`enter code here`
$table->timestamps();
});