LoginSignup
4
0

More than 3 years have passed since last update.

Laravel Eloquent の SELECT で CONCAT が使いたい

Last updated at Posted at 2020-08-04

やりたいこと

Eloquent 取得結果の頭に「'」をつけたい

やったこと

select する時に、 CONCAT を使用した

fuga テーブルを元に、 hoge テーブルを JOIN して、 hoge テーブルの tel カラムの頭に「'」を付与する。

DB::table('fuga')
->select(DB::raw('CONCAT("\'", hoge.tel) AS tel')
->join('hoge', 'hoge.fuga_id', '=', 'fuga.id')
->get()

ちゃんと頭に「'」ついてきたよ。

CONCATがうまく使えなくて、Eloquent の取得結果を加工する? でも元のオブジェクトに戻すにはどうしたらいいの? と迷走していました。
CONCAT使えて本当に良かった。

4
0
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
4
0