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

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

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

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

        Laravel:让 Eloquent 创建嵌套 SELECT 的正确方法

        时间:2023-09-22
          <tbody id='l4Yrh'></tbody>
        1. <tfoot id='l4Yrh'></tfoot>

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

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

                <legend id='l4Yrh'><style id='l4Yrh'><dir id='l4Yrh'><q id='l4Yrh'></q></dir></style></legend>
                • <bdo id='l4Yrh'></bdo><ul id='l4Yrh'></ul>

                  本文介绍了Laravel:让 Eloquent 创建嵌套 SELECT 的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我试图雄辩地生成的查询是

                  The query I am trying to get eloquent to generate is

                  SELECT *, (SELECT COUNT(comment_id) FROM comment AS c WHERE c.approved=true AND c.blog_fk=b.blog_id) AS comment_count FROM blog AS b
                  

                  这是结果

                  blog_id |  title            | author       | blog           | image            | tags    | created             | updated             | comment_count
                  --------|-------------------|--------------|----------------|------------------|---------|---------------------|---------------------|--------------
                       21 | A day..           | dsyph3r      | Lorem ipsum... | beach.jpg        | symf... | 2014-12-22 19:14:34 | 2014-12-22 19:14:34 | 2
                       22 | The pool ..       | Zero Cool    | Vestibulum ... | pool_leak.jpg    | pool,.. | 2011-07-23 06:12:33 | 2011-07-23 06:12:33 | 10
                       23 | Misdirection...   | Gabriel      | Lorem ipsum... | misdirection.jpg | misd... | 2011-07-16 16:14:06 | 2011-07-16 16:14:06 | 2
                       24 | The grid ...      | Kevin Flynn  | Lorem commo... | the_grid.jpg     | grid... | 2011-06-02 18:54:12 | 2011-06-02 18:54:12 | 0
                       25 | You're either ... | Gary Winston | Lorem ipsum... | one_or_zero.jpg  | bina... | 2011-04-25 15:34:18 | 2011-04-25 15:34:18 | 2
                  

                  我目前使用 DB::select(DB::raw()) 运行它,这可能不是正确的方法.

                  I currently have this running by using DB::select( DB::raw()) which probably isn't the correct way to do this.

                  问题是什么才是能够产生这些结果的查询的正确方法?

                  The question is what is the proper way to get eloquent to produce the query that generates those results?

                  推荐答案

                  改用这个:http://softonsofa.com/tweaking-eloquent-relations-how-to-get-hasmany-relation-count-高效

                  对于嵌套的 select/join 语句,您需要:

                  $sub = Comment::selectRaw('count(comment_id) as count')
                         ->where('approved', '?')
                         ->where('comment.blog_fk', '?')
                         ->toSql();
                  
                  Blog::selectRaw(DB::raw("blog.*, ({$sub}) as comment_count"))
                         ->setBindings([true, DB::raw('blog.blog_id')], 'select')
                         ->get();
                  

                  或者干脆把所有东西都放在selectRaw中.

                  Or simply put everything in selectRaw.

                  这篇关于Laravel:让 Eloquent 创建嵌套 SELECT 的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Laravel 中的原始相关性查询.如何处理? 下一篇:Laravel (HasMany) 不检索值

                  相关文章

                  最新文章

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

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

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