3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

LaravelでLIKE BINARYを使う

3
Last updated at Posted at 2016-09-16

@abandoned この記事は役目を終えました

Laravel は 5.0 から、LIKE BINARY というオペレータをサポートしているので、普通に使えます。ありがとうございました!

>>> Specie::where('fulltextindex','like','%ベンケイソウ%')->first()->fulltextindex
=> "ベンケイソウ科 コチレドン"

>>> Specie::where('fulltextindex','like','%ぺンケイソウ%')->first()->fulltextindex
=> "ベンケイソウ科 コチレドン" // [ペ]なのに[べ]にヒットする

>>> Specie::where('fulltextindex','like binary','%ベンケイソウ%')->first()->fulltextindex
=> "ベンケイソウ科 コチレドン"

>>> Specie::where('fulltextindex','like binary','%ぺンケイソウ%')->first()->fulltextindex
PHP error:  Trying to get property 'fulltextindex' of non-object on line 1

以下は、古いのでスルーでお願いします。

サポートしていません

MySQLのLIKEで、濁点などのあいまい検索を適用せずに、厳密に一致させるときに使える LIKE BINARY ですが、

$user->where('name','like binary','%よしだ%');

としたいところですが、どうもサポートしていません。

解決策1. whereRaw を使う

困ったときの生SQL。

$user->whereRaw('name like binary \'%よしだ%\' ');

解決策2. サポートさせてしまう

http://git.foo.mobi/laravel/framework/commit/76f6da4dab1e9e4847362a2b2b0c428e25805f0c
コチラを参考に。
Laravelのソースコードを書き換えます。

src/Illuminate/Database/Query/Builder.php
class Builder {
	...
 	protected $operators = array(
 		'=', '<', '>', '<=', '>=', '<>', '!=',
		'like', 
		'like binary', // ←この行を追加 
		'not like', 'between', 'ilike',
 		'&', '|', '^', '<<', '>>',
 		'rlike', 'regexp', 'not regexp', 
 		'~', '~*', '!~', '!~*', 'similar to',

元のソースコードを書き換えるとバージョンアップなどにうまく対応できなさそうで、ちょっと勇気いります……。

3
3
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?