Configure your file .env
:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=banco
DB_USERNAME=root
DB_PASSWORD=
DB_CONNECTION1=mysql_gps
DB_HOST1=192.168.1.125
DB_PORT1=3306
DB_DATABASE1=meubanco
DB_USERNAME1=root
DB_PASSWORD1=senha
ie two settings, where the difference is the number 1, ie you can in the file .env
put as many as you want but, identify each of them.
Goes in the file app/config/database.php
:
<?php
return [
'default' => env('DB_CONNECTION', 'mysql'),
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
'mysql_gps' => [
'driver' => 'mysql',
'host' => env('DB_HOST1', '127.0.0.1'),
'port' => env('DB_PORT1', '3306'),
'database' => env('DB_DATABASE1', 'forge'),
'username' => env('DB_USERNAME1', 'forge'),
'password' => env('DB_PASSWORD1', ''),
'unix_socket' => env('DB_SOCKET1', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
two settings also pointing to the file settings .env
.
How to use this?
In the documentation on multiple bank configurations, if used normally, it would be:
//conexão padrão
$users = DB::connection('mysql')->select(...);
$users = DB::select(...);
or
//conexão remota
$users = DB::connection('mysql_gps')->select(...);
If you want to create it within model
eloquent just configure the protected $connection
the name of the connection, example:
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Unidade extends Model
{
protected $fillable = ['titulo'];
protected $primaryKey = 'id_unidade';
protected $connection = 'mysql_gps'; // configurando a conexão remota
}
Observing: the models
eloquent or DB
that are not configured take by default the configuration that is configured in,'default' => env('DB_CONNECTION', 'mysql'),
.
References
How are you doing? Put the code
– Diego Souza
See the database.php file as configured.
– Diego Souza
This Line means something: APP_ENV=local
– Carlos Alexandre R Ramos
@Carlosalexwe ran this setup on
.env
will work only for a database, what you really need to do???– novic
I am developing, but in the remote bank I receive information at all times and I need to test some events, and the location and only configuration, authentication and everything else
– Carlos Alexandre R Ramos
@Carlosalexwe can point out in the model a certain connection a standard for your system and one according to your need and on
.env
, can only have one configuration, because one overrides the other, already in config/database.php can have several and use in certain models– novic
Maybe I’ll try, more local the way it’s working, but I’ll try anyway
– Carlos Alexandre R Ramos