Support the ongoing development of Laravel.io →
posted 9 years ago
Eloquent
Last updated 1 year 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 1 year ago.
0
Solution

In logs model:

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

}
Last updated 1 year 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 1 year 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 1 year 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 1 year ago.
0

Sign in to participate in this thread!

Eventy

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?

Laravel.io

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

© 2024 Laravel.io - All rights reserved.