master

laravel/framework

Last updated at: 29/12/2023 09:22

ConnectsToDatabase.php

TLDR

This file contains the ConnectsToDatabase trait, which provides a method for creating a new database connection.

Methods (if applicable)

connect

Creates a new database connection using the provided parameters.

Classes (if applicable)

<?php

namespace Illuminate\Database\PDO\Concerns;

use Doctrine\DBAL\Driver\Connection as ConnectionContract;
use Illuminate\Database\PDO\Connection;
use InvalidArgumentException;
use PDO;

trait ConnectsToDatabase
{
    /**
     * Create a new database connection.
     *
     * @param  mixed[]  $params
     * @return \Illuminate\Database\PDO\Connection
     *
     * @throws \InvalidArgumentException
     */
    public function connect(array $params): ConnectionContract
    {
        if (! isset($params['pdo']) || ! $params['pdo'] instanceof PDO) {
            throw new InvalidArgumentException('Laravel requires the "pdo" property to be set and be a PDO instance.');
        }

        return new Connection($params['pdo']);
    }
}