Source: juststeveking-uk-astro.pages.dev

Tappable Query Scopes in Laravel
I recently saw an awesome tweet while browsing twitter which introduced this idea of tappable query scopes, and wanted to share it and dig into it a little more. The idea originally came from a PR on the laravel/framework repo in the comments https://github.com/laravel/framework/pull/42111#issuecomment-1116944244.

So what we have is an invokable class that acts like a callable that we can simply call to extend the query we are building. So in effect the above example could use the below: User::query()->tap(function (Builder $query) { $query->where('email', 'taylor@laravel.com'); })->get(); But what if we want to call multiple scopes on a query?

Builder::macro('filter', function (...$scopes): Builder { collect($scopes)->each(function ($scope) { $this->tap($scope); }); return $this; }); So we are extending the Builder and adding the filter method.
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