Work with two databases in Laravel

Asked

Viewed 2,043 times

6

I’m creating an application that needs to connect to two different databases. A database I am creating (Mysql) and a database I received ready that I am only allowed to query (SQL Server). The purpose of the application is to query the data of the SQL Server database, make operations with this data and store them in the Mysql database

I’m very new to Laravel and I don’t know if it’s possible to work with both banks. Has anyone ever done it or knows if it works?

UPDATING:

I tried to do as I suggested link but makes a mistake.

Database file.php:

'default' => 'mysql',
'connections' => array(


        'mysql' => array(
            'driver'    => 'mysql',
            'host'      => 'localhost',
            'database'  => 'matriculas',
            'username'  => '*****',
            'password'  => '*****',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),

        'sqlsrv' => array(
            'driver'   => 'sqlsrv',
            'host'     => '10.31.24.2',
            'database' => 'SIGA',
            'username' => '******',
            'password' => '*****',
            'prefix'   => '',
        ),

    ),

the Controller:

class MatriculasController extends BaseController{

        public function formulario(){

        $teste= DB::connection('sqlsrv')->select('select * from supervisor.PESSOA');

        dd($teste);

    }

}

The mistake:

Erro no servidor (500)

UPDATE 2:

I realized that the problem is with the driver "sqlsrv" that does not have on the server. The server is a Centos 6 with PHP 5.4.45. I don’t know how to install this driver for this version of PHP, I can’t find the corresponding RPM package. Does anyone have any idea what to do??

  • 4

    I think this article can help you: this one in English: http://fideloper.com/laravel-multiple-database-connections

  • You wrote conection with a n only. It is connection.

  • 1

    @Deesouza, really! But still gave problem, I wrote correctly and when loading the page appears "Error on the server (500)"

  • This line is already set to true. It still shows no error. If I error the syntax or anything else shows the error correctly, in this case gives error on the server.

  • Nothing appears :(

  • Without the line $teste= DB::connection('sqlsrv')->select('select * from supervisor.PESSOA'); works perfectly.

  • Yes, the name is exactly as I wrote in the query

  • Works with Mysql

  • From what I understand, these Xtensions are for windows. I’m using Linux. If it is because of the connection with Sqlserver, everything is ok, I have another application on the server that uses the mssql function and works fine.

  • I found the problem, I will update the question.

  • Managed to solve this problem, @Amandalima ?

  • 1

    I got it in "gambiarra" mode. I made my own connection class using mssql. I found that the sqlsrv driver does not work on the server I am working on and I have no way to change the version of PHP installed

  • To install the driver, you only need to download it on the Microsoft website (php_sqlsrv_pdo and php_sqlsrv), save it in the PHP/ext folder, open the PHP configuration file, the php.ini, create a new reference extension=php_sqlsrv_pdo and extension=php_sqlsrv. Restart the server.

Show 8 more comments
No answers

Browser other questions tagged

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