laraveleloquentorm

Defining Database Schema in Laravel Eloquent (db.schema error)


I am integrating Laravel ORM into a framework and am stuck at the error "Target class [db.schema] does not exist.". I am using the following code:

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

Error is called before here, the below is just for reference.

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

The error seems to suggest db.schema needs setting up but I don't know Laravel well enough to determine where I am to do this.

Can anyone help?

The stack trace is below:

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

I have tried various combinations of the code (it is quite amazing to see how things change) I just don't know their relevance or enough to say what the problem is.


Solution

  • I needed to do this:

    $conn = $app['db']->getConnection()->setSchemaGrammar(new \Illuminate\Database\Schema\Grammars\MySqlGrammar());