Laravel 多个数据库的多态关系
Laravel polimorphic relation for multiple databases
我正在尝试在两个数据库表之间建立多态关系。
数据库 1:销售报告
数据库 2:ThreadConnection
在数据库 1 的 SaleReport 下
public function relates(){
return $this->morphMany(ThreadConnection::class, 'relation');
}
在数据库 2 的 ThreadConnection 上
public function relation(){
return $this->morphTo();
}
但是当我试图得到这样的关系时
$salereport = App\SaleReport::find(1);
$salereport->relates
Illuminate/Database/QueryException with message 'SQLSTATE[42S02]:
Base table or view not found: 1146 Table 'database1.thread_connections'
doesn't exist (SQL: select * from `thread_connections` where
`thread_connections`.`relation_id` = 484694 and
`thread_connections`.`relation_id` is not null and
`thread_connections`.`relation_type` = App/SaleReport)'
我也试过将数据库连接定义到ThreadConnection
$this->setConnection('database2')->morphTo();
但这也不管用!
任何建议都适用
正如我之前在评论中所述,
读取错误:
Base table or view not found: 1146 Table 'database1.thread_connections'
意思是 Laravel 正在寻找模型 ThreadConnection 的数据库 1 连接(数据库 1 应该是默认连接,如果模型引用另一个连接,它应该明确设置)
在线程连接中添加:
protected $connection = 'database2';
将解决问题。
我正在尝试在两个数据库表之间建立多态关系。
数据库 1:销售报告 数据库 2:ThreadConnection
在数据库 1 的 SaleReport 下
public function relates(){
return $this->morphMany(ThreadConnection::class, 'relation');
}
在数据库 2 的 ThreadConnection 上
public function relation(){
return $this->morphTo();
}
但是当我试图得到这样的关系时
$salereport = App\SaleReport::find(1);
$salereport->relates
Illuminate/Database/QueryException with message 'SQLSTATE[42S02]:
Base table or view not found: 1146 Table 'database1.thread_connections'
doesn't exist (SQL: select * from `thread_connections` where
`thread_connections`.`relation_id` = 484694 and
`thread_connections`.`relation_id` is not null and
`thread_connections`.`relation_type` = App/SaleReport)'
我也试过将数据库连接定义到ThreadConnection
$this->setConnection('database2')->morphTo();
但这也不管用!
任何建议都适用
正如我之前在评论中所述,
读取错误:
Base table or view not found: 1146 Table 'database1.thread_connections'
意思是 Laravel 正在寻找模型 ThreadConnection 的数据库 1 连接(数据库 1 应该是默认连接,如果模型引用另一个连接,它应该明确设置)
在线程连接中添加:
protected $connection = 'database2';
将解决问题。