Limiting a With?

Posted 6 months ago by mprythero

To be honest, I'm not quite sure where I'm supposed to start with this so if there is any additional clarification needed, feel free to ask.

I have a query on my contactController:

        $contacts = Contact::whereHas('donations', function ($query) use ($request){
                        $query->whereMonth('date_received', $request->month)->whereYear('date_received', $request->year);
                    })->with('donations')->get();

And technically this does work, as it does return all the contacts that had a donation sometime during the month and year period given. But my problem arises when I try to list out the donations as it lists all of that contact's donations. I'd like to only list out the donations that match up with the query above.

At the moment, my blade is like this:

@foreach($contacts as $contact)
{{$contact->surname}}

@foreach($contact->donations as $donation)
{{$donation->id}}
{{$donation->item_id}}
@endforeach

@endforeach

Now I know the above will return all of the donations for that customer, this was merely the format that I would like to have it set up in a report. I know that this type of filtering would likely be somewhere in the controller, so if anyone can help, I'd really appreciate it!

Please sign in or create an account to participate in this conversation.