4
I’ve researched several places, and they all end up leading to that 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 login time, such as financial check and others.
I receive 3 values per POST: "company", "login" and "password". I create a variable $empresa
, which is first used in the bank banco-1
. Validations made, the same variable needs to be checked in the bank financeiro
. After a positive return, then I access the system bank and check the login and 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 is just a variable. Laravel returns the error saying that the table "clients" does not exist in the "main" bank, ie the DB::connection('banco-1')
did not connect in the database-1 but in the main, and still the query ran.
It also follows how the config/database.php file is (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,
],
]
I am forgetting something?
Post the connection configuration file, I think I know what it is!
– novic
If nothing else works, and you only need to access these two banks in this single place, you could use the same PDO (or a preferred bank connector) in these two queries. At least until you can figure it out by Laravel himself.
– Daniel
@Virgilionovic I edited for you to take a look. Daniel thanks for the tip, I’ll do just that. But I wanted to make it work properly because I need this same scheme working on other systems.
– Cleiton Oliveira
@Cleitonoliveira was made an answer, you were using wrong, is because of this, something else in Eloquent itself can change to be easier, avoid using this in Controller, only in the last case even ... The answer explains how to solve.
– novic