Level 70
@kodac Does it work?
$cutoffDate = Carbon::now()->subMonths(2); // Get the cutoff date
$ticketsToDelete = Ticket::whereHas('ticketMessages', function ($query) use ($cutoffDate) {
// Find tickets where the latest message is older than 2 months
$query->where('created_at', '<', $cutoffDate)
->orderBy('created_at', 'desc')
->limit(1); // Ensures only the latest message is checked
})->get();
2 likes