Rick
Rick

Reputation: 711

Is it possible to use Laravel Facades with Illuminate Database Standalone?

I'm following this README and am curious if it's possible to get the following to work:

$capsule = new Illuminate\Database\Capsule\Manager;

$capsule->addConnection([
    'driver'    => 'mysql',
    'host'      => 'localhost',
    'database'  => 'database',
    'username'  => 'root',
    'password'  => 'password',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
]);

$capsule->setAsGlobal();
$capsule->bootEloquent();

# can Facades be used like this somehow?
var_dump(DB::connection()->getPdo());
var_dump(Hash::make('hash'));

I get the following error which is expected because the scripts not loading any Facades but I'm not sure how to tweak things to work:

Fatal error: Uncaught Error: Class 'DB' not found

I've also tried out the accepted answer here but still no luck (not even sure I'm using it correctly?)..

$container = new Illuminate\Container\Container();
Illuminate\Support\Facades\Facade::setFacadeApplication($container);
$container->singleton('db', 'Illuminate\Support\Facades\DB');
class_alias('Illuminate\Support\Facades\DB', 'DB');

Which gives me the following error:

Fatal error: Uncaught Error: Call to undefined method Illuminate\Support\Facades\DB::connection()

Adding "use DB;" up top still gives the following error:

Fatal error: Uncaught Error: Class 'DB' not found

Adding "use Illuminate\Support\Facades\DB;" up top gives the following error:

Fatal error: Uncaught RuntimeException: A facade root has not been set.

Any suggestions? Or is it just that Facades cannot be used with Illuminate Database standalone?

Upvotes: 2

Views: 1750

Answers (1)

Rick
Rick

Reputation: 711

Well after all that fooling around I finally figured it out..

composer.json

{
    "require": {
        "illuminate/database": "^5.7",
        "illuminate/hashing": "^5.7"
    }
}

bootstrap.php

require 'vendor/autoload.php';

$app = new Illuminate\Container\Container();
Illuminate\Support\Facades\Facade::setFacadeApplication($app);

$app->singleton('db', function () use ($app) {
    $capsule = new Illuminate\Database\Capsule\Manager;

    $capsule->addConnection([
        'driver'    => 'mysql',
        'host'      => 'localhost',
        'database'  => 'database',
        'username'  => 'root',
        'password'  => 'password',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ]);

    $capsule->setAsGlobal();
    $capsule->bootEloquent();

    return $capsule;
});

$app->singleton('hash', function () use ($app) {
    return new Illuminate\Hashing\HashManager($app);
});

class_alias(Illuminate\Support\Facades\DB::class, 'DB');
class_alias(Illuminate\Support\Facades\Hash::class, 'Hash');

index.php

require '../bootstrap.php';

var_dump(DB::connection()->getPdo());
var_dump(Hash::make('password'));

# object(PDO)#16 (0) {}
# string(60) "$2y$10$aevTHr94mYoh9PBlJz43EuT2qJLCKkNrOLNpKLhbjGzlqpMS6YVBG"

Upvotes: 2

Related Questions