The Eloquent Query Classes pattern organizes complex or reusable database queries in Laravel by wrapping them in small, focused classes with a single `handle()` method. Unlike the Repository Pattern, these classes don't abstract away Eloquent — they give important business queries a dedicated name and home. The post covers when to use query classes vs. local scopes, how to structure them in small and large apps, whether to return a Builder or execute the query directly, how to handle write operations, and how to test them with Pest and database factories. Practical guidelines include naming classes after business questions, keeping only one public method, and avoiding over-abstraction.

16m read timeFrom wendelladriel.com
Post cover image
Table of contents
IntroductionThe Problem With Queries EverywhereWhat is an Eloquent Query Class?Query Classes Are Not RepositoriesWhy Not Just Use Local Scopes?A Practical StructureReturning Builders From HandleQuery Classes Can Write TooWhen to Use Query ClassesWhen Not to Use Query ClassesHow to Test Query ClassesA Few Practical GuidelinesConclusion
95.2K Impressions8 Comments