我需要构建一个可以有多个关系的任务对象
A Task can have these relations (note even though some say required they are actually optional)
-- itemA_required // this can have many entries
-- itemA_optional // this can have many entries
-- itemB_required // this can have many entries
-- itemB_optional // this can have many entries
-- itemC_required // this can have many entries
-- itemC_optional // this can have many entries
-- itemD_required // this can have many entries
-- itemD_optional // this can have many entries
然后我有这样的表
task -- id|name|description
itemA -- id|name|description
itemB -- id|name|description
itemC -- id|name|description
itemD -- id|name|description
task_itemA_required -- id|task_id| itema_id
task_itemA_optional -- id|task_id| itema_id
task_itemB_required -- id|task_id| itemb_id
task_itemB_optional -- id|task_id| itemb_id
task_itemC_required -- id|task_id| itemc_id
task_itemC_optional -- id|task_id| itemc_id
task_itemD_required -- id|task_id| itemd_id
task_itemD_optional -- id|task_id| itemd_id
与此相关的模型
task
itemA
itemB
itemC
itemD
task_itemA_required
task_itemA_optional
task_itemB_required
task_itemB_optional
task_itemC_required
task_itemC_optional
task_itemD_required
task_itemD_optional
任务模型有这样的方法
public function ItemARequired()
{
return $this->hasMany('Models\ItemARequired');
}
public function ItemAOptional()
{
return $this->hasMany('Models\ItemAOptional');
}
我的问题是
这是最好的方法吗?大量的模型并没有使这看起来非常“雄辩”。
如果这是正确的方法,我如何查询任务,以便它返回所有关系以及与相关项目一起的id,名称和描述?这是否需要在Task模型中为每种关系使用类似的东西?我尝试过各种各样的变化,并且没有让它发挥作用。我想我并不真正理解这种雄辩的做法。
public function ItemARequired()
{
return $this->hasManyThrough('App\ItemA', 'App\task_itemA_required');
}
希望我在这里写的内容是有道理的。