Logic:
A company
has many users
, the users
can create alerts
, thus, many alerts
belong to one user
. Each alert
also has a location
relation, where one alert belongs to one location.
Because this is quite a vast query, I have achieved this (I think successfully!) through eager loading.
What I want to achieve is, for the company that the authenticated user belongs to, get all of the other users that works for the same company, along with all of their alerts (including location).
public function getAdminIndex()
{
$company_id = User::find(Auth::user()->id)
->companies()
->first()
->id;
$alerts = Company::with('users.alerts.location')
->where('id', '=', $company_id)
->get();
$this->layout->content = View::make('agents.admin.index',
array('alerts' => $alerts));
}
My only problem is now is trying to print the relevant information out. I want to print the 'reference' column, within the alerts table, first within the foreach loop:
{{ $alert->users->alerts->reference}}
but I keep getting
ErrorException Undefined property: Illuminate\Database\Eloquent\Collection::$alerts
My logic is, the function would go companies->users->alerts->... which is how they're reference within their respective models.
If I echo out $alert
, all of the information is there, in one huge unreadable jSON format!
Any help interpreting how to meander through a queries data would be hugely appreciated.