Reputation: 1412
I have this problem. In Laravel, I have 3 Models, ie:
class Department{
public function coordinations(){
return $this->has_many('Coordination');
}
}
class Coordination{
public function sites(){
return $this->has_many('Site');
}
}
class Site{
public function assets(){
return $this->has_many('FAssets');
}
}
class FAsset{}
I want to display all the asset grouped by Departments. I tryied to do something like:
@foreach($dependencias as $dep)
<tr>
<td colspan="8" style="width:100%">Dependencia: {{ $dep->code }} {{ $dep->description }}</td>
</tr>
@foreach($dep->coordinations()->sites()->assets()->get() as $asset)
...
But I got the "Method [sites] is not defined on the Query class" error. I was looking for the eager loading way but I dont see how it could work with an extra level on the relationships.
Also, I was considering to querying all the assets and filtering the assets given the actual coordination, but I think that this is going to take a lot of time to load...
Thanks in advance
Upvotes: 0
Views: 1248
Reputation: 1026
class Deparment{
public function department_assets(){
$assets = array();
foreach($this->has_many('Coordination')->get() as $coordination){
foreach($coordination->sites()->get() as $site){
foreach($site->assets() as $asset)
{
$assets[] = $asset;
}
}
}
return $assets;
}
}
Now we can...
@foreach($dep->department_assets() as $asset)
Upvotes: 1