本文介绍了雄辩的whereRaw无法使用绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a json column in my database with french characters. So when I use:

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

找不到名称为Funéraire之类重音的作业的结果.我可以通过使用whereRaw:

It is not finding results for jobs that has an accent in the name like Funéraire. I can accomplish what I want by adding a collate in the query using the whereRaw:

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

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

However, when I use bindings in my whereRaw method:

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

我收到数据库错误:

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

Just wondering why it's not working when I'm passing it with bindings.

推荐答案

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

You need to convert the binding to UTF-8:

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

这篇关于雄辩的whereRaw无法使用绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 18:24