* // Get the default database connection * $connection = DB::connection(); * * // Get a specific database connection * $connection = DB::connection('mysql'); * * * @param string $connection * @return Database\Connection */ public static function connection($connection = null) { if (is_null($connection)) $connection = Config::get('database.default'); if ( ! array_key_exists($connection, static::$connections)) { if (is_null($config = Config::get('database.connections.'.$connection))) { throw new \Exception("Database connection [$connection] is not defined."); } $connector = Connector\Factory::make($config); static::$connections[$connection] = new Connection($connection, $config, $connector); } return static::$connections[$connection]; } /** * Begin a fluent query against a table. * * This method primarily serves as a short-cut to the $connection->table() method. * * * // Begin a fluent query against the "users" table * $query = DB::table('users'); * * // Equivalent call using the connection table method. * $query = DB::connection()->table('users'); * * // Begin a fluent query against the "users" table for a specific connection * $query = DB::table('users', 'mysql'); * * * @param string $table * @param string $connection * @return Database\Query */ public static function table($table, $connection = null) { return static::connection($connection)->table($table); } /** * Magic Method for calling methods on the default database connection. * * This provides a convenient API for querying or examining the default database connection. * * * // Run a query against the default database connection * $results = DB::query('select * from users'); * * // Equivalent call using the connection instance * $results = DB::connection()->query('select * from users'); * */ public static function __callStatic($method, $parameters) { return call_user_func_array(array(static::connection(), $method), $parameters); } }