如何将此SQL转换为Laravel查询生成器代码?

时间:2019-07-17 09:35:12

标签: mysql laravel-5

这是我的SQL:

select
  m.id,
  m.first_name,
  GROUP_CONCAT(mt.name) membership_types
from
  members m
  left join expert_memberships em on em.member_id = m.id
  left join membership_types mt on mt.id = em.membership_type_id
group by
  m.id,
  m.first_name

1 个答案:

答案 0 :(得分:0)

使用此:

 $members = DB::table('members')
    ->leftJoin('expert_memberships', 'members.id', '=', 'expert_memberships.member_id')
    ->leftJoin('membership_types', 'membership_types.id', '=', 'expert_memberships.membership_type_id')
    ->select('members.id', 'members.first_name', DB::raw('GROUP_CONCAT(membership_types.name) membership_types'))
    ->groupBy('members.id', 'members.first_name')
    ->get();

group_concat您已以DB :: raw的身份通过,否则它将在select中不起作用。