0

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.

  • If you have a 1 to 1 relation you should never put data in different tables – php_nub_qq Apr 23 '14 at 20:21
  • That's not always true, @php_nub_qq, sometimes there are good reasons to use 1:1 relations: http://stackoverflow.com/questions/517417/is-there-ever-a-time-where-using-a-database-11-relationship-makes-sense – Bram Apr 23 '14 at 20:28
  • @Bram that's some mumbo jumbo IMHO. Basically the two arguments that made any sense what so ever were to "save space by not picking up unnecessary data" which doesn't need commenting and to increase security of the data. I don't believe that separating the data in two tables would make any significant security improvements. However that's just my opinion. – php_nub_qq Apr 23 '14 at 20:39

1 Answers1

0

Your solution looks very complicated to me. If you split up what you ask:

What I want to achieve is,

for the company that the authenticated user belongs to,

<?php
$company = Auth::user()->company;

get all of the other users that works for the same company,

<?php
$other_users = $company->users()->where('id', '!=', Auth::user()->id)->get();

along with all of their alerts (including location).

<?php
$other_users->load('alerts.locations');

Or, combined:

<?php
$other_users = $company->users()
    ->where('id', '!=', Auth::user()->id)
    ->with('alerts.locations')
    ->get();

About your other question:

If I echo out $alert, all of the information is there, in one huge unreadable jSON format!

Try dd($alert->toArray())

Bram
  • 4,232
  • 20
  • 23