Table des matières

Database

La configuration des bases de données se trouvent dans le fichier app/config/database.php

Définissez votre connexion par défaut

	'default' => 'mysql',
 
	'connections' => array(
 
		'sqlite' => array(
			'driver'   => 'sqlite',
			'database' => __DIR__.'/../database/production.sqlite',
			'prefix'   => '',
		),
 
		'mysql' => array(
			'driver'    => 'mysql',
			'host'      => 'localhost',
			'database'  => 'mybdd',
			'username'  => 'moi',
			'password'  => 'blabla',
			'charset'   => 'utf8',
			'collation' => 'utf8_unicode_ci',
			'prefix'    => 'myproject_',
		),
 
		'pgsql' => array(
			'driver'   => 'pgsql',
			'host'     => 'localhost',
			'database' => 'database',
			'username' => 'root',
			'password' => '',
			'charset'  => 'utf8',
			'prefix'   => '',
			'schema'   => 'public',
		),
 
		'sqlsrv' => array(
			'driver'   => 'sqlsrv',
			'host'     => 'localhost',
			'database' => 'database',
			'username' => 'root',
			'password' => '',
			'prefix'   => '',
		),
 
	),

Pour plusieurs base de données

Même principe, configurez autant de connexion que vous le voulez

	'default' => 'mysql',
 
	'connections' => array(
 
		'mysql' => array(
			'driver'    => 'mysql',
			'host'      => 'localhost',
			'database'  => 'mybdd',
			'username'  => 'moi',
			'password'  => 'blabla',
			'charset'   => 'utf8',
			'collation' => 'utf8_unicode_ci',
			'prefix'    => 'myproject_',
		),
		'mysql2' => array(
			'driver'    => 'mysql',
			'host'      => 'localhost',
			'database'  => 'mybdd2',
			'username'  => 'moi',
			'password'  => 'blabla',
			'charset'   => 'utf8',
			'collation' => 'utf8_unicode_ci',
			'prefix'    => 'myproject_',
		),
 
		'mysql3' => array(
			'driver'    => 'mysql',
			'host'      => 'localhost',
			'database'  => 'mybdd3',
			'username'  => 'moi',
			'password'  => 'blabla',
			'charset'   => 'utf8',
			'collation' => 'utf8_unicode_ci',
			'prefix'    => 'myproject_',
		),
 
		'pgsql' => array(
			'driver'   => 'pgsql',
			'host'     => 'localhost',
			'database' => 'database',
			'username' => 'root',
			'password' => '',
			'charset'  => 'utf8',
			'prefix'   => '',
			'schema'   => 'public',
		),
 
 
	),

Ensuite, dans votre code, vous pouvez spécifier votre connexion comme ceci pour les query builder

$user = DB::connection('mysql3')->table('users')->where('id', '=', 1)->first();

ou

$user = User::on('connection-name')->find(1);

Dans les models eloquent

class Toto extends Eloquent {
    protected $connection = 'mysql2';
}

Débugguer

Don’t use →toSql() for debugging. Use DB::getQueryLog() instead

http://forumsarchive.laravel.io/viewtopic.php?id=16874

Laravel 5.3 : error

Si vous avez ce genre d’erreur, pour une requête toute banale

>>> \App\Charge::where('id','>',40)->groupBy('gid')->get()
Illuminate\Database\QueryException with message 'SQLSTATE[42000]: Syntax error or access violation: 1055 'dev.charges.id' isn't in GROUP BY (SQL: select * from `charges` where `id` > 40 group by `gid`)'
>>> 

Il suffit de modifier la config strict ⇒ false de votre connexion mysql

        'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'dev'),
            'username' => env('DB_USERNAME', ''),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => false,
            'engine' => null,
        ],