Eloquent whereRaw 不使用绑定

Eloquent whereRaw is not working with bindings

我的数据库中有一个包含法语字符的 json 列。所以当我使用:

App\Job::where('name->fr', 'like', '%Fune%')->count();

找不到名称中带有重音符号的职位的结果,例如 Funéraire。我可以通过使用 whereRaw:

在查询中添加整理来完成我想要的
App\Job::whereRaw('json_unquote(json_extract(`name`, \'$."fr"\')) LIKE \'%Fune%\' collate utf8mb4_general_ci')->count();

但是,当我在 whereRaw 方法中使用绑定时:

App\Job::whereRaw('json_unquote(json_extract(`name`, \'$."fr"\')) LIKE ? collate utf8mb4_general_ci', ['%Fune%'])->count();

我收到一个数据库错误:

COLLATION 'utf8mb4_general_ci' is not valid for CHARACTER SET 'binary' (SQL: select count(*) from jobs where json_unquote(json_extract(name, '$."fr"')) LIKE %Fune% collate utf8mb4_general_ci)

只是想知道为什么当我通过绑定传递它时它不起作用。

中国

如果数据库有一个 JSON 列,您可以直接在该列上触发类似查询,因为 JSON 是一个字符串,并且像 %text% 一样搜索字符串中的文本。

您不必为了找到相似的名字而经历这么多麻烦。

您的查询可能看起来像这样,

App\Job::where('name_of_column_of_json', 'like', '%Fune%')->count();

您将获得所需的输出。

点击它让我们知道它是否有效。

您需要将绑定转换为 UTF-8:

App\Job::whereRaw(
    'json_unquote(json_extract(name, \'$."fr"\')) LIKE convert(? using utf8mb4)', 
    ['%Fune%']
)