Christian
Christian

Reputation: 427

laravel/php - group by id but still get the every row of data

this is my table

 patient_id  |  vaccine_id
     7              2
     7              3

I want it to be displayed like this on my html

patient id: 7, vaccine id: 2,3

I have this query on my 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();

but it's displaying

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

I have tried using groupby('patient_id');

but it's showing like this patient id: 7, vaccine id: 2 it doesnt display the other vaccine id

Upvotes: 1

Views: 466

Answers (1)

Mayank Pandeyz
Mayank Pandeyz

Reputation: 26258

You have to use group_concat(vaccine_id) with groupby('patient_id') to get the desired format like:

->selectRaw('GROUP_CONCAT(vaccine_id)')

Upvotes: 1

Related Questions