【发布时间】:2017-11-01 19:25:54
【问题描述】:
我在使用 orderByRaw 函数时遇到了 Eloquent/QueryBuilder 的问题,我得到异常列不存在,但该列已经存在于数据库中。
这是一个例外:
SQLSTATE[42883]: Undefined function: 7 ERROR: function field(integer, integer) does not exist
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
数据库中的状态列是整数。 这是我的代码:
$orderedStatuses = implode(',', [4]);
$users->orderByRaw(\DB::raw("FIELD(status, $orderedStatuses)"));
...另外,如果我使用 users.status 我得到了同样的错误。
有人可以帮我吗?
谢谢
【问题讨论】:
-
orderByRaw已经需要原始 SQL。你也不需要在里面使用DB::raw。 -
此外,对于 fubar 所写的内容,MySQL FIELD() 函数接受字符串作为参数。显然,您正在传递整数。错误消息没有说明您提到的不存在的列。这是关于错误使用的功能。
标签: database laravel eloquent laravel-query-builder