Case-sensitive where statement in laravel

女生的网名这么多〃 提交于 2021-02-17 15:17:37

问题


How can I do a case-sensitive string match with laravel?


SELECT * FROM `invites` WHERE `token`='OGwie2e2985tOEGewgu23hUFs'

Can be done as

Invite::where('token',$token)->first()


If I want a case-sensitive match I need to use a statement like this (or similar, as far as I know):

SELECT * FROM `invites` WHERE BINARY `token`='OGwie2e2985tOEGewgu23hUFs'

My best guess would be:

Invite::whereRaw("BINARY `token`='{$token}'")->first()

but then my input is not going through a prepared statement, right?


回答1:


You'll need to use DB::raw(), perhaps something like

Invite::where(DB::raw('BINARY `token`'), $token)->first();

or alternatively:

Invite::whereRaw("BINARY `token`= ?",[$token])->first()



回答2:


A little bit late but still wouldn't this be a better alternative?

Invite::whereRaw("BINARY `token`= ?", array($token))->first()


来源:https://stackoverflow.com/questions/25494849/case-sensitive-where-statement-in-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!