想请问一个关于Eloquent多表关联的一个问题
目前有以下表:
表A有:id,book_id
表B有:book_id,zuozhe_id
表C有:book_id,fanyi_id
表D有:zuozhe_id,zuozhe_name
表E有:fanyi_id,fanyi_name
其实B表和C表就是中间表那种概念。
因为一本书有多个作者,同时,一本书也有多个人参与翻译。想通过A表的book_id获取到该本书的所有zuozhe_name以及fanyi_name,以下是我弄的,虽然没有报错,但是没有出数据。想请教一下,问题出在哪了?
A model
public function bTable()
{
return $this->hasMany(BModel::class, 'book_id', 'book_id');
}
public function cTable()
{
return $this->hasMany(CModel::class, 'book_id', 'book_id');
}
B model
public function aTable()
{
return $this->belongsTo(AModel::class, 'book_id', 'book_id');
}
public function dTable()
{
return $this->belongsTo(DModel::class, 'zuozhe_id', 'zuozhe_id');
}
C model
public function aTable()
{
return $this->belongsTo(AModel::class, 'book_id' ,'book_id');
}
public function eTable()
{
return $this->belongsTo(DModel::class, 'fanyi_id', 'fanyi_id');
}
D model
public function bTable()
{
return $this->hasMany(BModel::class, 'narrator_id', 'narrator_id');
}
E model
public function cTable()
{
return $this->hasMany(BModel::class, 'fanyi_id', 'fanyi_id');
}
控制器的内容
$builder = AModel::with('bTable.dTable','cTable.eTable');
return Grid::make($builder, function (Grid $grid) {
$grid->column('zuozhe_name');
$grid->column('fanyi_name');
}
非常感谢回答
@LiamHao 大佬,已经加到帖子里面去了,麻烦再帮忙看看
关联的键都在 hasMany 第二个参数