I have already researched in several places, and they all lead to this link . But I could not work using the methods given in the link.
The problem: I have 3 banks configured in config / database.php: "main", "bank-1" and "financial", and I need to use all 3 in the same controller. The system only accesses one of them, the other 2 only serve for validations at the time of login, such as financial verification and others.
I get 3 values per POST: "company", "login" and "password". I create a $empresa
variable, which is first used in the banco-1
database. Validations made, the same variable needs to be checked in the financeiro
database. After a positive return, then yes I access the system bank and check the login and the password.
class LoginController extends Controller
{
public function entrar(Request $request){
$usuario = new \App\usuario();
$login = $request->usuario;
$senha = $request->senha;
$empresa = $request->empresa;
$cliente = DB::connection('banco-1')
->table('clientes')
->select('endereco','banco','nomecompleto','empresa','codigo','cnpj')
->where('empresa', '=',$empresa)->get();
Where $cliente
is not a model, it's just a variable. Laravel returns the error saying that the "clients" table does not exist in the "main" database, that is, DB::connection('banco-1')
did not connect in bank-1 but in the main, and still the query ran.
The following is the config / database.php file (the sensitive data removed, of course):
'default' => env('DB_CONNECTION', '-'),
'connections' => [
'principal' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '-'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', '-'),
'username' => env('DB_USERNAME', '-'),
'password' => env('DB_PASSWORD', '-'),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
'banco-1' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '-'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', '-'),
'username' => env('DB_USERNAME', '-'),
'password' => env('DB_PASSWORD', '-'),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
'financeiro' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '-'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', '-'),
'username' => env('DB_USERNAME', '-'),
'password' => env('DB_PASSWORD', '-'),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
]
Am I forgetting anything?