6
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】セルを連結して検索

Posted at

Laravel6.0

登録したユーザーの氏名(family_name)と名前(last_name)を連結してLike検索をしたいときなど。

データベース:クエリビルダ 6.x Laravel

時々、クエリの中でSQLを直接使用したいことがあります。エスケープなしのSQLを使用する場合はDB::rawメソッドを使用します。

DB::rawメソッドはエスケープ処理がされていないのでSQLインジェクションに注意。

CONCAT関数を使用する。
MySQL :: MySQL 5.6 リファレンスマニュアル :: 12.5 文字列関数

 User::where(DB::raw('CONCAT(family_name, last_name)'), 'like', '%'.$text.'%')->get();

参考

【Laravel】EloquentクエリでCONCAT関数を使う | 84LIFEブログ

6
3
0

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
6
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?