在雄辩中获得多重关系 [英] Getting multiple relationships in Eloquent

查看:175
本文介绍了在雄辩中获得多重关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

模型的基本误解雄辩(Laravel之外)



我以前的问题,ceejayoz帮助我指出了正确的方向,但这导致我基本上是一个新的问题。



我正在研究一个简单的化学品跟踪数据库,并使用化学品表格来跟踪大部分信息,但是我有几个字段开始作为枚举,但我意识到这不会奏效。现在有单独的公司,房间和位置的表格。所有这些额外的表格只包括一个id和字段,所以公司是:



1 | 'FISHER'



2 | 'BDH'



等。



我可以做

  $ company = chemical :: find(4) - > company; 

例如,这将给我这个化学品的公司的名称,但我正在尝试要做的是显示每个化学品中所有信息的表格,以及相关联的公司名称,房间,位置。
我只是不确定如何完成这个。



我如何甚至可以获得所有化学品的关联公司?

  $ chemicals = company :: all() - >公司; 

不行,我可以看到为什么。



$ pre> $ chemicals = chemical :: all();

foreach($ chemicals as $ chem){
echo $ chem-> company。 < br />;
}

会给我关联的公司,那很棒,但那我去哪里从那里的综合表?

解决方案

你不指定你的表 / code>,房间位置化学品。但是,如果它们都是 belongsTo 类型关系,那么我相信你正在寻找 Eager Loading


有时可以渴望加载关系您查询父模型。急于加载会减轻 N + 1查询问题


要加载多个关系,您可以执行以下操作:

  $ chemicals = chemical :: with('company','room','location') - > get(); 

如果您使用的是模板引擎,如Twig或Blade(希望您是),则可以将 $ chemicals 直接传递给模板。否则,您可以按照您的问题所示,通过 $ chemicals 进行迭代:

  echo< table>< thead>< tr> 
< th>化学ID< / th>
< th>化学名称< / th>
& th>公司名称< / th>
< th>位置名称< / th>
< th>房间名称< / th>
< / tr>< / thead> < TBODY>中;

foreach($ chemicals as $ chem){
echo< tr>< td> {$ chem-> id}< / td>
& td> {$ chem-> name}< / td>
< td> {$ chem-> company-> name}< / td>
< td> {$ chem-> location-> name}< / td>
< td> {$ chem-> room-> name}< / td>
< / tr> ;
}
echo< / tbody>< / table>;

请注意,雄辩中的惯例是将您的模型类名称(化学品,而不是 chemicals )。


Fundamental misunderstanding of model in Eloquent (Outside of Laravel)

My previous question that ceejayoz helped point me in the right direction for, but that has led me to what is basically a new question.

I'm working on a simple chemical tracking database and have it structured with a chemicals table which tracks most of the info but I have a few fields that started out as Enums but I realized that would not work for. There are now separate tables for company, room, and location. All of these additional tables include just an id and the field so company is:

1 | 'FISHER'

2 | 'BDH'

etc.

I can do

$company = chemical::find(4)->company;

for example and that will give me the name of the company for that chemical, but what I am trying to do is display a table with all of the information in each chemical, as well as the associated company name, room, location. I am just not sure as to how to accomplish this.

How would I even get the associated company for all the chemicals?

$chemicals = company::all()->company;

Does not work and I can see why.

$chemicals = chemical::all();   

foreach($chemicals as $chem) {
    echo $chem->company . "<br />";
}  

Will get me the associated companies and that's great, but then where do I go from there in terms of the comprehensive table?

解决方案

You don't specify how your tables company, room, and location are related to chemicals. However, if they are all belongsTo type relationships, then I believe you are looking for Eager Loading:

Eloquent can "eager load" relationships at the time you query the parent model. Eager loading alleviates the N + 1 query problem.

To eager-load multiple relationships, you can do something like:

$chemicals = chemical::with('company', 'room', 'location')->get();

If you are using a templating engine like Twig or Blade (which hopefully you are), you can then pass your $chemicals directly to the template. Otherwise, you can iterate through $chemicals as you demonstrated in your question:

echo "<table><thead><tr>
      <th>Chemical ID</th>
      <th>Chemical Name</th>
      <th>Company Name</th>
      <th>Location Name</th>
      <th>Room Name</th>
      </tr></thead><tbody>";

foreach($chemicals as $chem) {
    echo "<tr><td>{$chem->id}</td>
              <td>{$chem->name}</td>
              <td>{$chem->company->name}</td>
              <td>{$chem->location->name}</td>
              <td>{$chem->room->name}</td>
          </tr>";
}
echo "</tbody></table>";

Please also notice that the convention in Eloquent is to capitalize your model class names (Chemicals, not chemicals).

这篇关于在雄辩中获得多重关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆