0

I've two databases.

wordpress: database1

mysql: database2

database1 has table wp_posts (Post.php model)

database2 has table news_tag(NewsTag.php model) and publishers(Publisher.php) model.

wp_posts has:
id
news_tag has: 
id
post_id 
publisher_id 
publishers have:
id 

I'm trying to access the publisher from the wp_posts. How can I achieve it?

In the Post.php model, I've tried something like:

    public function publisher() {
    return $this->hasManyThrough(
     Publisher::Class,
     NewsTag::Class,
     'post_id',
     'publisher_id',
     'id',
     'id',
     'id');

}

Maybe I'm not being able to access multiple databases in same model? I'm getting this error:

SQLSTATE[42S02]:
 Base table or view not found: 1146 Table 'db_news.wp_publishers' doesn't exist
 (SQL: select `wp_publishers`.*, `wp_news_tags`.`post_id` as `laravel_through_key` 
from `wp_publishers` inner join `wp_news_tags` on `wp_news_tags`.`id` = 
`wp_publishers`.`publisher_id` where `wp_news_tags`.`post_id` in (?) and 
`wp_publishers`.`deleted_at` is null and `wp_news_tags`.`deleted_at` is null)

Nish Dekardo
  • 329
  • 2
  • 11

1 Answers1

0

If the connection the two databases is correct as mentioned in the comments (if not you can see @Abdulla Nilam answer it describes how to set up multiple database connections), then the only problem is your table name is not being picked up by Laravel.

Try to manually define it:

class Publisher extends Model
{
    /**
 * The table associated with the model.
 *
 * @var string
 */
protected $table = 'wp_publishers';

}
Makdous
  • 1,447
  • 1
  • 12
  • 24