SETUP
Model
public function totalValidated()
{
return $this->hasOne('user')->where('validated', '=', "1")
->selectRaw('district_id, count(*) as total')->groupBy('district_id');
}
Controller
$districts = District::select(array('id', 'name'))
->with(array('usersCount','totalValidated'))
->get();
return View::make('districts.index', compact('districts'));
I have the following data returned:
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
}
}
View
@foreach ($districts as $district)
<tr>
<td>{{$district->name}}</td><td>{{$district->total_validated->total}}<td>
<tr>
@endforeach
Question
How do i display the "total" in "total_validated" per district? The above only gets me a
"Trying to get property of non-object"
which i know is wrong, but i'm not sure how to get the value out of that nested array.
Thanks!
Not sure what's up with blade, but plain php worked
<?php echo($district['totalValidated']['total']) ?>