diff --git a/eloquent.md b/eloquent.md index 0a5cd0d4135..a898963533d 100644 --- a/eloquent.md +++ b/eloquent.md @@ -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) @@ -1373,6 +1374,17 @@ Once the expected arguments have been added to your scope method's signature, yo $users = User::ofType('admin')->get(); + +## 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(); + ## Comparing Models