I've got a Laravel query working the way I'd like with min()
and groupBy()
:
$certificates = CertificateRecord::whereHas('learner', function($query) use($user) { $query->where('user_id', '=', $user->id); })
->selectRaw('*,min(type) as type')
->groupBy('certificate_template_id')
->with('issuer','certificateType')
->paginate(10);
But when I add this to the mix...
->when($request->type, function ($query) use ($request) {
return $query->where('type', '=', $request->type);
})
The previously grouped results are somehow ignored while performing this where()
and it returns all ungrouped rows.