App\services\Database::prepareConnection PHP Method

prepareConnection() public static method

Try to connect to the database with given config.
public static prepareConnection ( array $config = null ) : mysqli
$config array
return mysqli
    public static function prepareConnection($config = null)
    {
        $config = $config ?: config('database.connections.mysql');
        // use error control operator to hide warnings
        @($conn = new \mysqli($config['host'], $config['username'], $config['password'], $config['database'], $config['port']));
        if ($conn->connect_error) {
            throw new InvalidArgumentException($conn->connect_error, $conn->connect_errno);
        }
        return $conn;
    }

Usage Example

 /**
  * Bootstrap any application services.
  *
  * @return void
  */
 public function boot(Request $request)
 {
     View::addExtension('tpl', 'blade');
     // check dotenv
     if (!file_exists(base_path('.env'))) {
         throw new PrettyPageException(trans('setup.file.no-dot-env'), -1);
     }
     try {
         // check database config
         Database::prepareConnection();
     } catch (\Exception $e) {
         throw new PrettyPageException(trans('setup.database.connection-error', ['msg' => $e->getMessage()]), $e->getCode());
     }
     // skip the installation check when setup or under CLI
     if (!$request->is('setup') && !$request->is('setup/*') && PHP_SAPI != "cli") {
         $this->checkInstallation();
     }
 }