LoginSignup
0
1

More than 3 years have passed since last update.

[Laravel] joinで結合先のカラムの取得がものすごく簡単でびっくりした話

Posted at
User.php
publict function content()
{
   $this->join('content', 'content.user_id', 'user.user_id')
}

とすると、発行されるSQLは

SELECT *
from user
inner join content
on 
content.user_id = user.user_id
where user.deleted_at is NULL

となる。多分(間違っていたらすみません)

ここでcontentの主キ-も(content_id)取得したいとする。
そこでselect()の出番

次のようにする

$this->join('content', 'content.user_id', 'user.user_id')
     ->select('user.*', 'content.content_id')

すると

SELECT user.*,
       content.content_id
from user
inner join content
on 
content.user_id = user.user_id
where user.deleted_at is NULL

簡単に結合先のカラム取得できた。

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