Laravel在withCount方法上使用where子句 [英] Laravel using where clause on a withCount method

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

问题描述

我正在尝试使用这段代码在laravel雄辩的查询生成器的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'where子句'中的未知列'upvotes_count'(SQL:select ,(select count()from upvotes where upvotes .. upvoteable_id = posts.idupvotes.upvoteable_type = App \ Post),来自postsupvotes_count其中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 = App\Post) 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]:找不到列:1247不支持引用'upvotes_count'(项目列表中的前向引用)(SQL:选择,(从upvotes中选择count()),其中.upvoteable_id = posts.idupvotes.upvoteable_type = App \ Post和upvotes_count> 5)as upvotes_count from posts其中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 = App\Post 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();

AND

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

错误.

SQLSTATE [42S22]:找不到列:1054'where子句'中的未知列'upvotes_count'(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.

推荐答案

您可以使用以下方法获得所需的结果:

You can achieve requested result by using:

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

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

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