Skip to content

Instantly share code, notes, and snippets.

@brain2xml
Forked from pekhee/connection.php
Created September 1, 2021 06:27
Show Gist options
  • Save brain2xml/f5e34449eba935535a2f543ecb91c614 to your computer and use it in GitHub Desktop.
Save brain2xml/f5e34449eba935535a2f543ecb91c614 to your computer and use it in GitHub Desktop.
Setup database connection for Eloquent outside of Laravel.
<?php // Namespace DB;
use Illuminate\Database\Capsule\Manager as Capsule;
class Connection {
public function __construct()
{
$this->capsule = new Capsule;
// Same as database configuration file of Laravel.
$this->capsule->addConnection([
'driver' => 'sqlite',
'database' => __DIR__.'/database.sqlite',
'prefix' => '',
], 'default');
$this->capsule->bootEloquent();
$this->capsule->setAsGlobal();
// Hold a reference to established connection just in case.
$this->connection = $this->capsule->getConnection('default');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment