Category: Laravel, Laravel

Laravel recently introduced the ability to detect n+1 issues caused by lazily loading Eloquent relationships. This meant that simply killing execution because of an n+1 issue was not an approach I wanted to roll out.

Let's look at how we can use a lottery to handle n+1 detection in our Laravel applications.

This custom handler meets our needs nicely in that it doesn't stop execution, so it doesn't interrupt developers when they are in the flow, it doesn't flood Sentry and use all of our quota, and it allows us to incrementally tighten the odds as we address n+1 issues throughout the application.

I saw this "lottery" approach in Laravel itself and just reused it for this problem.
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners