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

标签 php mysql laravel-5

这是我的 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/

相关文章:

php - php 中带有换行符的 MySQL 输出

mysql - SQL WHERE 子句 : where age between x and x?

php - Laravel whereSlug 不工作

laravel-5 - 将数据库表导入到 algolia

php - Laravel 5.2 : how to set a variable in blade template, 没有 php 标签?

php - 我似乎无法在页面上显示我的查询结果?

php - 根据 Woocommerce 中的购物车项目运输类别计数更改运输类别

php - 协助在 php echo 中使用 £ 符号

mysql - 此 INSERT SQL 查询中的错误是什么?

mysql - phpmyadmin 外键问题