Good evening, I am trying to make a query of different connection tables, I have a connection that is the usual one called mysql
and I have created another called apolo
, I need to do a join with both.
$usuarios = DB::connection('apolo')->table('users')
->join('clientes AS client', 'users.intranet_id', '=', 'client.id')
->select('users.*', 'client.name')
->get();
As you can see I connect to the connection of apolo
and I want to do a join with my table of clients of my usual connection mysql
that I am doing wrong, I have done it like this many times without problem, I don't know what I am doing wrong.
Cheers
It throws me this error:
Base table or view not found: 1146 Table 'apolo2021.clientes' doesn't exist
You are looking for the customers table in the database which is not.
Edition
I'm trying like this but either, I don't know if it's because there is a table in both databases users
.
$prueba = DB::Connection('apolo')
->table('users')
->join(DB::Connection('mysql'))
->table('clientes', 'users.intranet_id', '=', 'clientes.id')
->get();
dd($prueba);
But it returns me this error:
Too few arguments to function Illuminate\Database\Query\Builder::join(), 1 passed in
The join function does not work for you since, according to the documentation, that function does not receive the connection as a parameter:
Join Eloquent
You can create a model of the Users table indicating that it should use the apollo connection.
Then you make the relationship with the clients table one by one (I understand that each user is associated with a single client):
One to one relationship in eloquent
You can also map the relationship in Clients, it would be:
When getting the model you should be able to use the client() or user() function, depending on which model you searched for: