【发布时间】:2020-05-09 05:11:15
【问题描述】:
这些是我的模型
class Posts extends Model {
protected $connection = 'connection_1';
public function comments()
{
$model = new Comments();
$model->setConnection($this->connection);
return $this->hasMany($model, 'comments_id');
}
}
class Comments extends Model {
protected $connection = 'connection_1';
public function author()
{
$model = new Authors();
$model->setConnection($this->connection);
return $this->hasOne($model, 'id', 'authors_id');
}
public function post()
{
$model = new Posts();
$model->setConnection($this->connection);
return $this->hasOne($model, 'id', 'posts_id');
}
}
class Authors extends Model {
protected $connection = 'connection_1';
public function comments()
{
$model = new Comments();
$model->setConnection($this->connection);
return $this->hasMany($model, 'authors_id');
}
}
当我将此代码放入我的控制器时,评论模型已连接到 connection_2 但作者为空,我猜它不会更改与 connection_2 的连接
$posts = new Posts;
$posts->setConnection('connection_2');
$posts->load("comments.author")->get();
如何动态更改 Authors 模型连接,或者为什么 Authors 为空?
【问题讨论】:
标签: laravel eager-loading