Member-only story
Eager Loading Constraints That Quietly Return the Wrong Rows
Bob has no published posts but shows up in “users with published posts”. Why. 6 eager loading bugs + fixes, verified in PHP 8.3.
Here’s a data quality bug that survived production for months because everyone assumed the ORM was doing what the code looked like it said.
A SaaS platform has a dashboard showing “customers with active subscriptions”. Product manager wants a list: customer name, their subscription details, quick stats. Developer writes what looks obvious:
$customers = Customer::with(['subscriptions' => function ($q) {
$q->where('status', 'active');
}])->get();Reviewer sees it, thinks “loading customers with their active subscriptions”. Merged. Deployed. Dashboard works — shows customers, shows their subscriptions. Product manager happy.
Six months later, sales team asks marketing to email “all customers with active subscriptions” a product announcement. Marketing uses the same dashboard filter. Sends the email to the list they see.
Complaints start coming in. Customers who canceled subscriptions months ago received the email. Some customers who never had ANY subscription received it. Some got it twice. Some active customers didn’t get it at all.




