Skip to content

Commit

Permalink
update Database.php
Browse files Browse the repository at this point in the history
  • Loading branch information
juancristobalgd1 authored Nov 25, 2023
1 parent 8a2b466 commit ff4543c
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,25 @@
*/
class Database
{
private static $conection;
/**
* @var Illuminate\Database\Capsule\Manager
*/
private static $connection;

/**
* Create a new database connection for models
*
* @param string|null $driver
*/
public static function connect(string $driver = null)
{
// If there is already a connection, do nothing
if (isset(static::$connection)) return;

$config = config('/DataBase.php');

$driver = $driver ?? $config['db']['default'] ?? 'mysql';

$capsule = static::$conection = new Capsule;
$capsule = static::$connection = new Capsule;
$capsule->addConnection(
$config['db']['connections'][$driver]
);
Expand All @@ -39,21 +44,34 @@ public static function connect(string $driver = null)

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

// if (php_sapi_name() === 'cli') {
// Schema::$capsule = $capsule;
// }
}

/**
* Get database connection
* @return Illuminate\Database\Capsule\Manager
*/
public static function get()
public static function db()
{
return static::$conection;
if (static::$connection) {
return static::$connection;
}

static::connect();

return static::$connection;
}

/**
* Database disconnect
*/
public static function disconnect(string $driver)
{
return static::$conection->getConnection()->disconnect($driver);
return static::$connection
->getConnection()
->disconnect($driver);
}
}

0 comments on commit ff4543c

Please sign in to comment.