简体   繁体   English

在一个查询中访问多个关系的数据

[英]Accessing multiple relationships' data within one query

Logic: 逻辑:

A company has many users , the users can create alerts , thus, many alerts belong to one user . 一个company有许多users ,这些users可以创建alerts ,因此,许多alerts属于一个user Each alert also has a location relation, where one alert belongs to one location. 每个alert还具有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: 我想首先在foreach循环中在警报表中打印“参考”列:

{{ $alert->users->alerts->reference}}

but I keep getting 但我不断

ErrorException Undefined property: Illuminate\\Database\\Eloquent\\Collection::$alerts ErrorException未定义的属性: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. 我的逻辑是,该函数将进入company-> users-> alerts-> ...,这是它们在各自模型中的引用方式。

If I echo out $alert , all of the information is there, in one huge unreadable jSON format! 如果我回显$alert ,则所有信息都以一种巨大的无法读取的jSON格式存在!

Any help interpreting how to meander through a queries data would be hugely appreciated. 任何帮助解释如何遍历查询数据的帮助将不胜感激。

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! 如果我回显$ alert,则所有信息都以一种巨大的无法读取的jSON格式存在!

Try dd($alert->toArray()) 尝试dd($alert->toArray())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM