Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Case-sensitive where statement in laravel

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?

like image 306
Andy Fleming Avatar asked Aug 25 '14 21:08

Andy Fleming


People also ask

Is laravel case sensitive?

Laravel Routing Case-insensitive routeswill match a GET request to /login but will not match a GET request to /Login . In order to make your routes case-insensitive, you need to create a new validator class that will match requested URLs against defined routes.

How do I create a case sensitive query in MySQL?

When searching for partial strings in MySQL with LIKE you will match case-insensitive by default*. If you want to match case-sensitive, you can cast the value as binary and then do a byte-by-byte comparision vs. a character-by-character comparision. The only thing you need to add to your query is BINARY .


2 Answers

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()
like image 139
Mark Baker Avatar answered Sep 20 '22 03:09

Mark Baker


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

Invite::whereRaw("BINARY `token`= ?", array($token))->first()
like image 32
Just a Guest Avatar answered Sep 20 '22 03:09

Just a Guest