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

  2. <small id='CJ7RA'></small><noframes id='CJ7RA'>

    <legend id='CJ7RA'><style id='CJ7RA'><dir id='CJ7RA'><q id='CJ7RA'></q></dir></style></legend>

      <tfoot id='CJ7RA'></tfoot>
    1. 带有一些约束的 Eloquent 嵌套关系

      时间:2023-09-24
        <i id='DEjag'><tr id='DEjag'><dt id='DEjag'><q id='DEjag'><span id='DEjag'><b id='DEjag'><form id='DEjag'><ins id='DEjag'></ins><ul id='DEjag'></ul><sub id='DEjag'></sub></form><legend id='DEjag'></legend><bdo id='DEjag'><pre id='DEjag'><center id='DEjag'></center></pre></bdo></b><th id='DEjag'></th></span></q></dt></tr></i><div id='DEjag'><tfoot id='DEjag'></tfoot><dl id='DEjag'><fieldset id='DEjag'></fieldset></dl></div>

        <tfoot id='DEjag'></tfoot>

            <tbody id='DEjag'></tbody>
            • <small id='DEjag'></small><noframes id='DEjag'>

                <bdo id='DEjag'></bdo><ul id='DEjag'></ul>

                <legend id='DEjag'><style id='DEjag'><dir id='DEjag'><q id='DEjag'></q></dir></style></legend>
                本文介绍了带有一些约束的 Eloquent 嵌套关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我有以下三个表:

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

                所以,C表的数据属于B表的数据,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 嵌套关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何从原始对象创建 Eloquent 模型实例? 下一篇:Laravel 多对多加载相关模型计数

                相关文章

                最新文章

              • <legend id='l2qUW'><style id='l2qUW'><dir id='l2qUW'><q id='l2qUW'></q></dir></style></legend>

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

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

                    • <bdo id='l2qUW'></bdo><ul id='l2qUW'></ul>