BelongsToMany with two diffrent mysql connection

Posted 1 year ago by haizone

i have a 2 diffrent mysql connection

1 - mysql db

  • categories table

2 - articles db

  • article table
  • articles_categories table

i try to make a relationship with the two tables with a difrrent mysql conacction

but i get an error:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'articles.categories' doesn't exist (SQL: select `categories`.*, `articles_categories`.`article_id` as `pivot_article_id`, `articles_categories`.`category_id` as `pivot_category_id` from `categories` inner join `articles_categories` on `categories`.`id` = `articles_categories`.`category_id` where `articles_categories`.`article_id` = 7)

categories db is inside mysql table and article and articles_categories is inside the articles db connaction

this is my code:

  public function categories(){

return $this->belongsToMany('App\Models\Category','articles_categories','article_id','category_id');

}

i think the laravel try to find the categories table in articles db but the categories table is inside the mysql db

thanks

Please sign in or create an account to participate in this conversation.