How to make multiple database connections with Laravel 4.2

Asked

Viewed 169 times

1

I have an app written on Laravel Framework 4.2, rotating in the PHP 5.6, in the Operating System Windows 7, connected to a mysql database, there was the need to make additional connection in another database to get specific information for this application, but all the connection attempts I tried were unsuccessful and return the following error message:

Pdoexception could not find driver

Below are the settings I made to try a multiple connection.

Filing cabinet: database php.

<?php
return array(
    'fetch' => PDO::FETCH_CLASS,
    'default' => 'mysql',

    'connections' => array(
        'mysql' => array(
            'driver'    => 'mysql',
            'host'      => '192.168.10.10',
            'database'  => 'sistema',
            'username'  => 'root',
            'password'  => 'root',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),
        'sqlsrv' => array(
            'driver'   => 'sqlsrv',
            'host'     => '192.168.10.19',
            'database' => 'ceps',
            'username' => 'root',
            'password' => 'root',
            'prefix'   => '',
        ),
    ),

    'migrations' => 'migrations',
    'redis' => array(
        'cluster' => false,
        'default' => array(
            'host'     => '127.0.0.1',
            'port'     => 6379,
            'database' => 0,
        ),
    ),
);

Filing cabinet: Ceps.php

<?php
class Ceps extends Eloquent {
    protected $connection = 'sqlsrv';
    protected $table = 'ceps';
}

Filing cabinet: Homecontroller.php

<?php
class HomeController extends BaseController {
    public function index()
    {
        $cep = DB::connection('sqlsrv')->select('cep', '=', '01003000')->get();
        Log::info($cep);
        return View::make('home',['title' => 'Home','menu' => 'home']);
    }
}

I followed the guidelines available in the official documentation of Laravel 4.2, but I did not succeed in connecting to the second database.

  • https://stackoverflow.com/questions/31847054/how-to-use-multiple-databases-in-laravel See if this link can help you.

  • I had already read and tried the procedures of this reply, but it also did not work, I tried again, in case I had "lost" some procedure, but still did not work.

  • 1

    I saw that sometimes there is problem when connecting with sqlsrv, make a connection only with this bank to see if the problem can be the drive.

  • Your suggestion helped me find the solution to the problem, thank you.

1 answer

1


The error message quoted is because I don’t have the Microsoft SQL Server driver installed and configured with my PHP.

Download the driver pack separately on website of Microsoft according to the version of PHP that is running, with attention to detail if the version of PHP is NTS or TS.

In my case, I used the Drivers version 3.0.1 because my version of PHP is 5.6.40.

After downloading just run (with administrator permissions) the program for installation, accept the license terms:

inserir a descrição da imagem aqui

Click on Browse... and set the location of the PHP extension directory (within the directory where PHP was installed), and then click on OK:

inserir a descrição da imagem aqui

The extension files will be unzipped inside the directory ext, now you have to configure PHP to use the right extensions by editing the file php.ini including the following lines at the end of the file:

[PHP_SQLSRV_56_NTS]
extension=php_sqlsrv_56_nts.dll
[PHP_PDO_SQLSRV_56_NTS]
extension=php_pdo_sqlsrv_56_nts.dll

I restarted my Web server (IIS 6.1) and my application started working properly again.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.