• <small id='xaDZu'></small><noframes id='xaDZu'>

    <i id='xaDZu'><tr id='xaDZu'><dt id='xaDZu'><q id='xaDZu'><span id='xaDZu'><b id='xaDZu'><form id='xaDZu'><ins id='xaDZu'></ins><ul id='xaDZu'></ul><sub id='xaDZu'></sub></form><legend id='xaDZu'></legend><bdo id='xaDZu'><pre id='xaDZu'><center id='xaDZu'></center></pre></bdo></b><th id='xaDZu'></th></span></q></dt></tr></i><div id='xaDZu'><tfoot id='xaDZu'></tfoot><dl id='xaDZu'><fieldset id='xaDZu'></fieldset></dl></div>

      <bdo id='xaDZu'></bdo><ul id='xaDZu'></ul>
      <tfoot id='xaDZu'></tfoot>

      1. <legend id='xaDZu'><style id='xaDZu'><dir id='xaDZu'><q id='xaDZu'></q></dir></style></legend>

        具有某些约束的 Eloquent 嵌套关系

        Eloquent Nested Relation with Some Constraint(具有某些约束的 Eloquent 嵌套关系)

        <small id='A8pxa'></small><noframes id='A8pxa'>

          <tfoot id='A8pxa'></tfoot>

              <i id='A8pxa'><tr id='A8pxa'><dt id='A8pxa'><q id='A8pxa'><span id='A8pxa'><b id='A8pxa'><form id='A8pxa'><ins id='A8pxa'></ins><ul id='A8pxa'></ul><sub id='A8pxa'></sub></form><legend id='A8pxa'></legend><bdo id='A8pxa'><pre id='A8pxa'><center id='A8pxa'></center></pre></bdo></b><th id='A8pxa'></th></span></q></dt></tr></i><div id='A8pxa'><tfoot id='A8pxa'></tfoot><dl id='A8pxa'><fieldset id='A8pxa'></fieldset></dl></div>
                <tbody id='A8pxa'></tbody>
              • <bdo id='A8pxa'></bdo><ul id='A8pxa'></ul>
                <legend id='A8pxa'><style id='A8pxa'><dir id='A8pxa'><q id='A8pxa'></q></dir></style></legend>
                • 本文介绍了具有某些约束的 Eloquent 嵌套关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有以下三个表:

                  A
                  -------------
                  | id | name |
                  -------------
                  
                  B
                  --------------------
                  | id | A_id | name |
                  --------------------
                  
                  C
                  --------------------
                  | id | B_id | name |
                  --------------------
                  

                  所以,表C中的数据属于表B中的数据,而表A中的数据属于表A中的数据.现在,我想查询 C,同时还从 BA 检索数据,下面的代码就可以了.

                  So, the data in table C belongs to the data in table B which belongs to the data in table A. Now, I want to query C, while also retrieving data from B and A and the following code does the trick just fine.

                  C::with('B.A')->get();
                  

                  现在的问题是,我想用一些约束查询 C.这些约束之一是 Aid.我尝试了以下方法:

                  The problem now, is that I want to query C with some constraints. One of these constraints is the id of A. I've tried the following:

                  C::with(array('B.A' => function ($query)
                  {
                      $query->where('id', '=', $constraint);
                  }))->get();
                  

                  但似乎 Eloquent 将检索 C 中的所有行,甚至不考虑约束,除非它执行查询以检索表 A 中的数据.我该如何解决这个问题?我是否需要在 C 中添加另一个字段,即 A_id,并将 $constraint 与该字段匹配?

                  But it seems that Eloquent will retrieve all the rows in C without even taking the constraint into account, except when it's executing the query to retrieve data in table A. How do I get around this problem? Do I need to add another field in C, that is A_id, and match $constraint against that field?

                  推荐答案

                  您将 with() 方法与 SQL 的 JOIN 方法混淆了,这种情况经常发生.

                  You are confusing the with() method with SQL's JOIN and that happens a lot.

                  当你使用 Foo::with('bar')->where_something(1) 时,Laravel 将首先加载 Foo,然后,基于 Foo.bar_id,它会加载Bar.它的目的是告诉 Laravel 在组合查询中预先加载模型的依赖项,从而大大提高这些模型的迭代性能.

                  When you use Foo::with('bar')->where_something(1), Laravel will first load the Foo and then, based on Foo.bar_id, it will load the Bar. It serves the purpose of telling Laravel to eager load dependencies of your model on a combined query, greatly improving performance of iterations on those models.

                  如果你不使用它,应该执行以下查询:

                  If you don't use it, the following queries should be executed:

                  SELECT * FROM foos WHERE foos.something = 1;
                  SELECT * FROM bars WHERE bars.id = 30;
                  SELECT * FROM bars WHERE bars.id = 57;
                  SELECT * FROM bars WHERE bars.id = 134;
                  SELECT * FROM bars WHERE bars.id = 1096;
                  

                  另一方面,如果你使用它:

                  If you use it, on the other hand:

                  SELECT * FROM foos WHERE foos.something = 1;
                  SELECT * FROM bars WHERE bars.id IN (30, 57, 134, 1096); // Eager loading
                  

                  当您向该 with() 添加条件时,您只是限制了这些依赖项的即时加载,而不是第一个查询.

                  When you add a condition to that with(), you are only constraining the eager loading of those dependencies, and not the first query.

                  要实现你想要的,你需要使用 ->join().

                  To achieve what you want, you'll need to use ->join().

                  C::with(array('b', 'b.a'))
                   ->join('b', 'b.id', '=', 'c.b_id')
                   ->join('a', 'a.id', '=', 'b.a_id')
                   ->where('a.id', '=', $ID)
                   ->get('c.*');
                  

                  我已经包含了 with(),因为我不知道您是否需要访问 $c->b->a.如果不这样做,而您只需要 $c 数据,则可以删除 with(),因为它会不必要地查询 B 和 A.

                  I've included the with(), because I didn't know if you would need to access $c->b->a. If you don't, and you just need $c data, you can remove the with() since it will query for B's and A's unnecessarily.

                  这篇关于具有某些约束的 Eloquent 嵌套关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)

                          <tbody id='uNhmu'></tbody>

                          <bdo id='uNhmu'></bdo><ul id='uNhmu'></ul>
                          <tfoot id='uNhmu'></tfoot>
                        • <small id='uNhmu'></small><noframes id='uNhmu'>

                          <legend id='uNhmu'><style id='uNhmu'><dir id='uNhmu'><q id='uNhmu'></q></dir></style></legend>
                          1. <i id='uNhmu'><tr id='uNhmu'><dt id='uNhmu'><q id='uNhmu'><span id='uNhmu'><b id='uNhmu'><form id='uNhmu'><ins id='uNhmu'></ins><ul id='uNhmu'></ul><sub id='uNhmu'></sub></form><legend id='uNhmu'></legend><bdo id='uNhmu'><pre id='uNhmu'><center id='uNhmu'></center></pre></bdo></b><th id='uNhmu'></th></span></q></dt></tr></i><div id='uNhmu'><tfoot id='uNhmu'></tfoot><dl id='uNhmu'><fieldset id='uNhmu'></fieldset></dl></div>