gpt4 book ai didi

php - Laravel 5.6 Eloquent whereDate 和 WhereRaw 错误

转载 作者:太空宇宙 更新时间:2023-11-03 10:32:51 25 4
gpt4 key购买 nike

我正在尝试查找今天创建的所有记录,并以列 functional_id 的 1 开头。

我得到以下语法错误:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'WHERE functional_id LIKE "1%"' at line 1 (SQL: select count(*) as aggregate from image_requests where date(created_at) = 2019-01-24 00:00:00 and WHERE functional_id LIKE "1%")

我的 Eloquent 代码:

$counter = ImageRequest::whereDate('created_at', $date)->whereRaw('WHERE functional_id LIKE "1%"')->count();

最佳答案

这应该有效:

$counter = ImageRequest::whereDate('created_at', $date)
->where('functional_id', 'like', '1%')
->count();

我认为问题在于额外的 "。但是,在这种情况下不需要使用 whereRaw()where() 绰绰有余,而且不太容易出现这样的错误 :)

编辑:根据您的评论,如果您想要检查第十二个字符是否为 1,以下应该可行:

$counter = ImageRequest::whereDate('created_at', $date)
->whereRaw('SUBSTRING(funtional_id, 12, 1) = 1')
->count();

关于php - Laravel 5.6 Eloquent whereDate 和 WhereRaw 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54344295/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com