I have a working function, however, I'd like to limit the number of treatments, per consultant, that are returned.
Working Example:
Clinic::where('user_id', Auth::id())
->with('consultants.specialism', 'consultants.treatments')
->first();
Proposed (but not working) example:
Clinic::where('user_id', Auth::id())
->with('consultants.specialism')
->with(['consultants.treatments' => function ($query) {
$query->take(3);
}])
->first();
Unfortunately, the take
or limit
function, limits it to the total number of treatments returned.
What I would like is to limit each consultant's treatments to a maximum of 3, not the total.
How can I achieve this please?
treatments
should return a collection of treatments, right? Then you want to get only first 3... So what's the problem?->take(3)
does exactly that. – Laubin