Laravel Database

Laravel Database

Configuration

The database configuration for your application is located at config/database.php.

Read & Write Connections

'mysql' => [
    'read' => [
        'host' => ['192.168.1.1'],
    ],
    'write' => [
        'host' => ['196.168.1.2'],
    ],
    'sticky'    => true,
    'driver'    => 'mysql',
    'database'  => 'database',
    'username'  => 'root',
    'password'  => '',
    'charset'   => 'utf8mb4',
    'collation' => 'utf8mb4_unicode_ci',
    'prefix'    => '',
],

The proper connections will always be used whether you are using raw queries, the query builder, or the Eloquent ORM.

Using Multiple Database Connections

You may access each connection via the connection method on the DB facade.

$users = DB::connection('foo')->select(...);
$pdo = DB::connection()->getPdo();

Running Raw SQL Queries

Database Transactions

DB::transaction(function () {
    DB::table('users')->update(['votes' => 1]);

    DB::table('posts')->delete();
});

Laravel Database — Structure map

Clickable & Draggable!

Laravel Database — Related pages: