Support the ongoing development of Laravel.io →
posted 8 years ago
Eloquent
Last updated 4 months ago.
0
  • Short answer: No it is not, a User model only using 1 database connection a time.
  • Long answer: Yes, it is possible if you do it yourself.

Just for curious, why are you doing that?

Last updated 4 months ago.
0
Solution

In logs model:

class Log extend Eloquent
{
protected $connection = 'db2'; // config/database.php >> connections

}
Last updated 4 months ago.
0

We have an external log database for many apps.

If i define a new database connection in database.php and run a query like this

select * from log.logs

the query works fine. Even if i use joins but i want to use eloquent/query builder.

Last updated 4 months ago.
0

Oh! I misunderstand you. When you say 'database', then I think 'database server'. However I believe Eloquent does not supports this for now, as you can see in the db configuration: one database per connection so it's mean, at a time, a model has 1 connection, therefore, the relationship query will be built in the current database context.

Last updated 4 months ago.
0

Thank you @mejlo! It works like a charm. I only had to set the correct foreign keys on my relation.

@bomberman1990: Now you know it works. :p

Last updated 4 months ago.
0

Sign in to participate in this thread!

Cloudways

Your banner here too?

divdax divdax Joined 3 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

© 2022 Laravel.io - All rights reserved.