这是我的 table
patient_id | vaccine_id
7 2
7 3
我希望它在我的 html 上这样显示
患者编号:7,疫苗编号:2,3
我的 Controller 上有这个查询
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
最佳答案
您必须将 group_concat(vaccine_id)
与 groupby('patient_id')
一起使用才能获得所需的格式,例如:
->selectRaw('GROUP_CONCAT(vaccine_id)')
关于php - laravel/php - 按 id 分组但仍然获取每一行数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42361191/