Case-sensitive where statement in laravel

Andy Fleming picture Andy Fleming · Aug 25, 2014 · Viewed 18.4k times · Source

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?

Answer

Mark Baker picture Mark Baker · Aug 25, 2014

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()