Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Document afterQuery method #9584

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions eloquent.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
- [Query Scopes](#query-scopes)
- [Global Scopes](#global-scopes)
- [Local Scopes](#local-scopes)
- [After Query Hook](#after-query-hook)
- [Comparing Models](#comparing-models)
- [Events](#events)
- [Using Closures](#events-using-closures)
Expand Down Expand Up @@ -1373,6 +1374,17 @@ Once the expected arguments have been added to your scope method's signature, yo

$users = User::ofType('admin')->get();

<a name="after-query-hook"></a>
## After Query Hook

If you want to alter models you queried from the database, you probably would do that on the result returned by the Eloquent query. The code that makes those alterations lives outside your Eloquent query code in that case. If you want to keep that code within your query code, you can use the `afterQuery` method. The provided closure always receives a collection of models as its first parameter. You can make changes to those models or even remove models you don't need.

User::query()
->afterQuery(function (Collection $users) {
// Alter users ...
})
->get();

<a name="comparing-models"></a>
## Comparing Models

Expand Down