knubbe
4 months ago
235
1
Laravel

How to get latest records based on two foreign id

Posted 4 months ago by knubbe

I have a CenterService entity (table center_services) and there is two columns center_id and service_id.

I have an array of services ids and I need collection in return but without of duplication center_id and service_id. I only need last record of that combination.

This is how I try:

$requiredServices = Service::whereRequiredService(true)->pluck('id');

return CenterService::whereIn('service_id', $requiredServices)->latest()->get();

but it's just order that collection with all duplicate data.

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