T
T
Timur Valitov2017-06-23 14:19:28
Laravel
Timur Valitov, 2017-06-23 14:19:28

How to retrieve data from remote laravel database?

Hello!
I read the official documentation for laravel, I do not understand one point.
I have .env in my project, there is data for connecting to the mysql database.
I want to extract lines from another database, which is supposed to be installed even on a different server.
Judging by the official documentation, you can get them like this: But I don’t understand how to add the second database to the configuration correctly, because database.php, for example, defaults to 127.0.0.1 and so on. Tried to add another mysql connection to the connections array. For a request like: I get this:
$users = DB::connection('foo')->select(...);
'select * from users where active = ?', [1]

Base table or view not found: 1146

It turns out that the application is trying to execute a request in the default database.
It seems to me that the .env database.php file should be used in some other way, but I don't know/understand how. Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
imhuman, 2017-06-23
@TimurRamilovich

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

'foo' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST_FOO', '127.0.0.2'),
            'port' => env('DB_PORT_FOO', '3306'),
            'database' => env('DB_DATABASE_FOO', ''),
            'username' => env('DB_USERNAME_FOO', ''),
            'password' => env('DB_PASSWORD_FOO', ''),
            'unix_socket' => env('DB_SOCKET_FOO', ''),
            'charset' => 'utf8',
            'collation' => 'utf8_general_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ],

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question