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

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