我有两个模型(事件和预算)与雄辩的拉拉,
模型事件的预算实现
public function budgeting()
{
return $this->hasMany('App\Budgeting', 'id_event', 'id');
}我只想从预算中得到一栏“总计”
这个代码控制器从预算中什么也没有显示出来。
public function data()
{
$listdata = Event::orderBy('id', 'desc')->with(array('budgeting'=>function($query){
$query->select('total','item');
}))->get();
return $listdata;
}发布于 2015-12-04 16:59:23
它不起作用的原因是,您还需要选择id_event,以便雄辩地建立关系。没有id_event,雄辩者就不知道Budgeting属于哪个Event。
要遵循的一般规则是:每当您更改select查询并构建关系时,您必须至少选择关系ids。
像这样更改您的查询,它应该可以工作。
$listdata = Event::orderBy('id', 'desc')->with(array('budgeting'=>function($query){
$query->select(`id_event`, 'total','item');
}))->get();发布于 2015-12-04 11:56:34
在事件模型中创建另一个关系,只获取特定列
public function totalBudgeting()
{
return $this->hasMany('App\Budgeting', 'id_event', 'id')->select(['total','item']);
}https://stackoverflow.com/questions/34087703
复制相似问题