请教 Lumen 的多对多关系中,涉及跨库问题的时候,中间表指定 connection 无效的问题
数据库结构
users
用户表, 在jcusercenter
数据库
exams
考试表,在wgzr
数据库
exam_user
中间表,为一场考试指定参考学生 在wgzr
数据库
模型
exams表模型
class Exam extends Model
{
protected $connection = 'mysql';
public function students()
{
return $this->belongsToMany(User::class)->using(ExamUser::class);
}
}
users表在另一个数据库
class User extends Model
{
protected $connection = 'user_center';
protected $table='productimportuser';
}
中间表
class ExamUser extends Pivot
{
protected $connection = 'mysql';
}
然后当我需要指定参考学生的时候,执行代码: $exam->students()->sync($request->student_ids);
出现下面的错误.
Base table or view not found: 1146 Table 'jcusercenter.exam_user' doesn't exist (SQL: select * from `exam_user` where `exam_id` = 12)
当时遇到问题,查到类似的回答说是让创建中间表模型,在模型里面指定$connection
,我现在也是这么做的.但是查询的时候还是会在users
表所在的库中查询中间表.
想请教各位有没有什么解决方法?
发现给中间表指定数据库就可以了. :sweat_smile: