SETUP
模型
public function totalValidated()
{
return $this->hasOne('user')->where('validated', '=', "1")
->selectRaw('district_id, count(*) as total')->groupBy('district_id');
}
控制器
$districts = District::select(array('id', 'name'))
->with(array('usersCount','totalValidated'))
->get();
return View::make('districts.index', compact('districts'));
我返回了以下数据:
0: {
id: 1,
name: "Northern California",
total_validated: {
district_id: "1",
total: 3
}
},
1: {
id: 2,
name: "Southern California",
total_validated: {
district_id: "2",
total: 30
}
}
查看
@foreach ($districts as $district)
<tr>
<td>{{$district->name}}</td><td>{{$district->total_validated->total}}<td>
<tr>
@endforeach
问题
如何在每个区域的“total_validated”中显示“总计”?上面只给我一个
"Trying to get property of non-object"
我知道这是错误的,但我不确定如何从嵌套数组中获取值。
谢谢!
答案 0 :(得分:0)
不确定是什么用刀片,但普通的php工作
<?php echo($district['totalValidated']['total']) ?>