2

I have two models - User and Announcement, and they exist on two separate connections - mysql and intranet respectively. The User model hasMany Announcements and the Announcement belongsTo a single User.

In the Announcement model I have protected $connection = 'intranet' and in the User model I have protected $connection = 'mysql'. Originally I wasn't specifying the $connection property on the User model as it is the default for the application but added it in for testing.

Doing any sort of query works normally. As an example these work:

  • User::find(1)->announcements()->where('name', 'something')->get()
  • Announcement::find(2)->user

However, when using whereHas on the Announcements model I get the following SQL Error: Base table or view not found: 1146 Table 'intranet.associate' - associate being the User's table and intranet being the database connection specified in the Announcement model.

While I did find an answer to the question here. I am curious, though, as to why you're required to use ->from(..). Is this intended functionality? I could not find anything in the documentation about using from().

As a side note I was able to fix this "issue" by assigning the $table property in the __construct method in the User model:

__construct(array $attributes = []) {
    $this->table = env('DB_DATABASE').'.associate';
}

env('DB_DATABASE') being the default connection's database.

Wild Beard
  • 2,919
  • 1
  • 12
  • 24

0 Answers0