laravel / php - 按ID分组但仍然获取每一行数据

时间:2017-02-21 07:33:33

标签: php mysql laravel-5

这是我的表

 patient_id  |  vaccine_id
     7              2
     7              3

我希望它在我的html上显示如下

患者身份:7,疫苗ID:2,3

我在我的控制器上有这个查询

return App\Notification::join('patients', 'patients.PatientID', '=', 'notifications.patient_id')
                        ->join('parent_accounts', 'parent_accounts.ParentID', '=', 'patients.parent_id')
                        ->join('vaccines', 'vaccines.VaccineID', '=', 'notifications.vaccine_id')
                        ->select('parent_accounts.parent_phonenumber','patients.*','vaccines.vaccine_name')
                        ->where('due_date', $request->due_date)->get();

但它正在显示

patient id: 7, vaccine id: 2 , patient id: 7, vaccine id: 3

我尝试过使用groupby('patient_id');

但它显示为patient id: 7, vaccine id: 2 它没有显示其他疫苗ID

1 个答案:

答案 0 :(得分:1)

您必须使用group_concat(vaccine_id)groupby('patient_id')来获得所需的格式,如:

->selectRaw('GROUP_CONCAT(vaccine_id)')