When writing a query it's common to use closures when using https://laravel.com/docs/9.x/queries#advanced-where-clauses or https://laravel.com/docs/9.x/queries#logical-grouping in-order for the query to behave as expected. $users = DB::table('users') ->where('name', '=', 'John') ->where(function ($query) { $query->where('votes', '>', 100) ->orWhere('title', '=', 'Admin'); }) ->get(); Enter fullscreen mode With more complex queries which have numerous closures the repeated use of the $query argument can make readability awkward. $users = DB::table('users') ->where('name', '=', 'John') ->where(function ($query) { $query->whereHas('role', function ($query) { $query->whereIn('slug', ['leader', 'deputy']) ->whereHas('team', function ($query) { $query->whereIn( 'slug', ['sales', 'finance'], ); }); }); }) ->get(); Enter fullscreen mode

$query->whereIn( 'slug', ['sales', 'finance'], ); // If the query argument has been named, // then we immediately know which table it is 🙂 $teams->whereIn( 'slug', ['sales', 'finance'], ); Enter fullscreen mode
Newsletter

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

Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge. Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge.
Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Shirts painstakingly handcrafted by under-caffeinated developers. Shirts painstakingly handcrafted by under-caffeinated developers.
Community Partners