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

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.


EDIT

I received an answer that was very helpful in getting me past where I needed, which was deleted. The user made their first post, see here.

I now have the following:

use Illuminate\Database\Capsule\Manager as Capsule;
use \Illuminate\Container\Container as Container;               
use Illuminate\Support\Facades\Facade;

$app = new Container();

// create a new capsule inscance
$capsule = new Capsule();

$conn = $capsule->addConnection([
    'driver'    => 'mysql',
    'host'      => '127.0.0.1',
    'database'  => 'yf_testing',
    'username'  => 'yf_testing',
    'password'  => 'xxxxxxxxxx',
    '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();

$app->singleton('db.schema', function ($app) {
    return new \Illuminate\Database\Schema\Builder($app['db']->getConnection());
});

// Register the database manager in the container 
$app->singleton('db', function($app) use ($capsule) {
    return $capsule;
});

This works, because if I exit at the end there are no errors (phew!!!), but when calling Schema:: I run into issues:

Fatal error: Uncaught TypeError: Illuminate\Database\Schema\Blueprint::build(): Argument #2 ($grammar) must be of type Illuminate\Database\Schema\Grammars\Grammar, null given, called in /usr/local//path/to/file/vendor/illuminate/database/Schema/Builder.php on line 439 and defined in /usr/local//path/to/file/vendor/illuminate/database/Schema/Blueprint.php:106
Stack trace:
#0 /usr/local//path/to/file/vendor/illuminate/database/Schema/Builder.php(439): Illuminate\Database\Schema\Blueprint->build(Object(Illuminate\Database\MySqlConnection), NULL)
#1 /usr/local//path/to/file/vendor/illuminate/database/Schema/Builder.php(285): Illuminate\Database\Schema\Builder->build(Object(Illuminate\Database\Schema\Blueprint))
#2 /usr/local//path/to/file/vendor/illuminate/support/Facades/Facade.php(338): Illuminate\Database\Schema\Builder->create('chat_channels', Object(Closure))
#3 /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)
#4 /usr/local/apache/htdocs/domain.tld/defaults/core/chains/Testing/RunTests/RunTestsAction.class.php(72): SkipperMigrationsChat2024091315401851->up()
#5 /usr/local/apache/htdocs/domain.tld/controller/ExecutionContainer.class.php(514): App\Testing\RunTests\Action->execute(Object(App\Request\Console))
#6 /usr/local/apache/htdocs/domain.tld/filter/Execution.class.php(410): App\ExecutionContainer->runAction()
#7 /usr/local/apache/htdocs/domain.tld/filter/FilterChain.class.php(129): App\Filter\Execution->execute(Object(App\Filter\FilterChain), Object(App\ExecutionContainer))
#8 /usr/local/apache/htdocs/domain.tld/filter/Security.class.php(72): App\Filter\FilterChain->execute(Object(App\ExecutionContainer))
#9 /usr/local/apache/htdocs/domain.tld/filter/FilterChain.class.php(129): App\Filter\Security->execute(Object(App\Filter\FilterChain), Object(App\ExecutionContainer))
#10 /usr/local/apache/htdocs/domain.tld/controller/ExecutionContainer.class.php(297): App\Filter\FilterChain->execute(Object(App\ExecutionContainer))
#11 /usr/local/apache/htdocs/domain.tld/filter/Dispatch.class.php(44): App\ExecutionContainer->execute()
#12 /usr/local/apache/htdocs/domain.tld/filter/FilterChain.class.php(129): App\Filter\Dispatch->execute(Object(App\Filter\FilterChain), Object(App\ExecutionContainer))
#13 /usr/local/apache/htdocs/domain.tld/controller/Controller.class.php(241): App\Filter\FilterChain->execute(Object(App\ExecutionContainer))
#14 /usr/local/apache/htdocs/domain.tld/public/development/console.php(27): App\Controller->dispatch()
#15 {main}
  thrown in /usr/local//path/to/file/vendor/illuminate/database/Schema/Blueprint.php on line 106

Hopefully the original poster comes back!!


Solution

  • I needed to do this:

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