teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

3

誤字の修正

2020/02/29 20:56

投稿

Eggpan
Eggpan

スコア3287

title CHANGED
File without changes
body CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
  ### 発生している問題・エラーメッセージ
8
8
 
9
- ###### 1. whereRawメソッドの第2引数にバインド値を入れてしてみた場合
9
+ ###### 1. whereRawメソッドの第2引数にバインド値を入れた場合
10
10
  ```PHP
11
11
  $search_word = "tes't";
12
12
  User::whereRaw("name LIKE %?% COLLATE utf8mb4_unicode_ci", [$search_word])->get();

2

COLLATE の例が utf8mb4_xxx に統一されてなかったので修正しました。

2020/02/29 20:56

投稿

Eggpan
Eggpan

スコア3287

title CHANGED
File without changes
body CHANGED
@@ -9,14 +9,14 @@
9
9
  ###### 1. whereRawメソッドの第2引数にバインド値を入れてしてみた場合
10
10
  ```PHP
11
11
  $search_word = "tes't";
12
- User::whereRaw("name LIKE %?% COLLATE utf8_unicode_ci", [$search_word])->get();
12
+ User::whereRaw("name LIKE %?% COLLATE utf8mb4_unicode_ci", [$search_word])->get();
13
13
 
14
14
  $search_word = "tes't";
15
- User::whereRaw("name LIKE ? COLLATE utf8_unicode_ci", ['%' . $search_word . '%'])->get();
15
+ User::whereRaw("name LIKE ? COLLATE utf8mb4_unicode_ci", ['%' . $search_word . '%'])->get();
16
16
  ```
17
17
 
18
18
  ```SQL
19
- select * from `users` where name LIKE %tes't% COLLATE utf8_unicode_ci
19
+ select * from `users` where name LIKE %tes't% COLLATE utf8mb4_unicode_ci
20
20
  ```
21
21
  どちらも上記のSQLが生成され、シンタックスエラーとなってしまう。
22
22
 
@@ -25,10 +25,10 @@
25
25
  $search_word = "tes't";
26
26
 
27
27
  $search_word = str_replace(['\', "'", '_', '%'], ['\\', "\'", '\_', '\%'], $search_word);
28
- User::whereRaw("name LIKE '%{$search_word}%' COLLATE utf8_unicode_ci")->get();
28
+ User::whereRaw("name LIKE '%{$search_word}%' COLLATE utf8mb4_unicode_ci")->get();
29
29
  ```
30
30
  ```
31
- select * from `users` where name LIKE '%tes\'t%' COLLATE utf8_unicode_ci
31
+ select * from `users` where name LIKE '%tes\'t%' COLLATE utf8mb4_unicode_ci
32
32
  ```
33
33
  この場合SQL自体は想定通りとなってはくれるのですが、書き方がスマートでないです。出来ればエスケープなどはフレームワークに任せたいです。
34
34
 

1

SQL文が同じCOLLATEになってない部分があったので修正

2020/02/27 17:43

投稿

Eggpan
Eggpan

スコア3287

title CHANGED
File without changes
body CHANGED
@@ -25,10 +25,10 @@
25
25
  $search_word = "tes't";
26
26
 
27
27
  $search_word = str_replace(['\', "'", '_', '%'], ['\\', "\'", '\_', '\%'], $search_word);
28
- User::whereRaw("name LIKE '%{$search_word}%' COLLATE utf8mb4_unicode_ci")->get();
28
+ User::whereRaw("name LIKE '%{$search_word}%' COLLATE utf8_unicode_ci")->get();
29
29
  ```
30
30
  ```
31
- select * from `users` where name LIKE '%tes\'t%' COLLATE utf8mb4_unicode_ci
31
+ select * from `users` where name LIKE '%tes\'t%' COLLATE utf8_unicode_ci
32
32
  ```
33
33
  この場合SQL自体は想定通りとなってはくれるのですが、書き方がスマートでないです。出来ればエスケープなどはフレームワークに任せたいです。
34
34