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

    1. <small id='KjlbL'></small><noframes id='KjlbL'>

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

          <bdo id='KjlbL'></bdo><ul id='KjlbL'></ul>
      1. <tfoot id='KjlbL'></tfoot>

        Laravel 在 withCount 方法上使用 where 子句

        时间:2023-09-23

        <tfoot id='Qzemc'></tfoot>

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

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

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

                  本文介绍了Laravel 在 withCount 方法上使用 where 子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在尝试使用这段代码对 laravel 的 eloquent 查询构建器的 withCount 方法执行 where 子句.

                  I am trying to do a where clause on withCount method of laravel's eloquent query builder using this piece of code.

                  $posts = Post::withCount('upvotes')->where('upvotes_count', '>', 5)->get();
                  

                  这段代码给了我这个错误.

                  and this code is giving me this error.

                  SQLSTATE[42S22]:未找到列:1054 未知列upvotes_count"在where 子句"(SQL:select , (select count() from upvotes whereupvotes.upvoteable_id = posts.idupvotes.upvoteable_type = AppPost) as upvotes_count from posts where upvotes_count > 5)

                  SQLSTATE[42S22]: Column not found: 1054 Unknown column 'upvotes_count' in 'where clause' (SQL: select , (select count() from upvotes where upvotes.upvoteable_id = posts.id and upvotes.upvoteable_type = AppPost) as upvotes_count from posts where upvotes_count > 5)

                  所以我可以猜测的是 upvotes_count 没有被选中,因此没有找到该列,但是如果我执行这段代码.

                  So from what I can guess is that upvotes_count isn't selected and hence the column is not being found, BUT if I execute this piece of code.

                  $posts = Post::withCount('upvotes')->get();
                  

                  然后我得到这个输出.

                  {
                  "id": 1,
                  "user_id": 15,
                  "title": "Voluptatum voluptas sint delectus unde amet quis.",
                  "created_at": "2016-10-07 13:47:48",
                  "updated_at": "2016-10-07 13:47:48",
                  "upvotes_count": 7
                  },
                  {
                  "id": 2,
                  "user_id": 2,
                  "title": "Molestiae in labore qui atque.",
                  "created_at": "2016-10-07 13:47:48",
                  "updated_at": "2016-10-07 13:47:48",
                  "upvotes_count": 2
                  },
                  

                  这基本上意味着 upvotes_count 被选中,因此我真的很困惑如何解决这个问题.

                  Which basically means that upvotes_count is being selected, hence i am really confused about how to solve this problem.

                  (下面给出了我迄今为止尝试过的更多选项以及与之相关的相应错误.)

                  (More options that I tried so far are given below with the respective error associated to it.)

                  $posts = Post::where('id', $id)->withCount(['upvotes' => function($query) {
                          $query->where('upvotes_count', '>', 5);
                      }])->get();
                  

                  错误.

                  SQLSTATE[42S22]: Column not found: 1247 Reference 'upvotes_count' not supported (forward reference in item list) (SQL: select , (select count() from upvotes where upvotes.upvoteable_id = posts.id and upvotes.upvoteable_type = AppPost and upvotes_count > 5) as upvotes_count from posts where id =1)

                  SQLSTATE[42S22]: Column not found: 1247 Reference 'upvotes_count' not supported (forward reference in item list) (SQL: select , (select count() from upvotes where upvotes.upvoteable_id = posts.id and upvotes.upvoteable_type = AppPost and upvotes_count > 5) as upvotes_count from posts where id = 1)

                  代码.

                  $posts = Post::where('id', $id)->with(['upvotes' => function($query) {
                          $query->select('upvoteable_id AS upvotes_count');
                      }])->where('upvotes_count', '>', 5)->get();
                  

                  $posts = AppPost::where('id', $id)->with(['upvotes' => function($query) {
                          $query->selectRaw('upvoteable_id AS upvotes_count');
                      }])->where('upvotes_count', '>', 5)->get();
                  

                  错误.

                  SQLSTATE[42S22]:未找到列:1054 Unknown column 'upvotes_count' in 'where clause'(SQL:从 posts 中选择 * 其中 id = 1 和 <代码>upvotes_count > 5)

                  SQLSTATE[42S22]: Column not found: 1054 Unknown column 'upvotes_count' in 'where clause' (SQL: select * from posts where id = 1 and upvotes_count > 5)

                  <小时>

                  我只想在与父模型有关系的 count() 方法上使用 where 子句.


                  I just want to use where clause on a count() method which is in a relationship with a parent model.

                  推荐答案

                  您可以通过使用:

                  $posts = Post::withCount('upvotes')
                           ->having('upvotes_count', '>', 5)
                           ->get();
                  

                  这篇关于Laravel 在 withCount 方法上使用 where 子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:无法让 Laravel 助理工作 下一篇:在 Eloquent 中按自定义顺序对集合进行排序

                  相关文章

                  最新文章

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

                      <bdo id='fcS9Q'></bdo><ul id='fcS9Q'></ul>
                  1. <legend id='fcS9Q'><style id='fcS9Q'><dir id='fcS9Q'><q id='fcS9Q'></q></dir></style></legend>

                    <tfoot id='fcS9Q'></tfoot>
                  2. <small id='fcS9Q'></small><noframes id='fcS9Q'>