diff --git a/eloquent-collections.md b/eloquent-collections.md index 21d08dae2f..37c6e184c3 100644 --- a/eloquent-collections.md +++ b/eloquent-collections.md @@ -316,25 +316,7 @@ $users = $users->withoutAppends(); ## Custom Collections -If you would like to use a custom `Collection` object when interacting with a given model, you may add the `CollectedBy` attribute to your model: - -```php - $models - * @return \Illuminate\Database\Eloquent\Collection + * @var class-string<\Illuminate\Database\Eloquent\Collection<*, *>> */ - public function newCollection(array $models = []): Collection - { - $collection = new UserCollection($models); - - if (Model::isAutomaticallyEagerLoadingRelationships()) { - $collection->withRelationshipAutoloading(); - } - - return $collection; - } + protected static string $collectionClass = UserCollection::class; } ``` -Once you have defined a `newCollection` method or added the `CollectedBy` attribute to your model, you will receive an instance of your custom collection anytime Eloquent would normally return an `Illuminate\Database\Eloquent\Collection` instance. +Once you have defined the `$collectionClass` property on your model, you will receive an instance of your custom collection anytime Eloquent would normally return an `Illuminate\Database\Eloquent\Collection` instance. -If you would like to use a custom collection for every model in your application, you should define the `newCollection` method on a base model class that is extended by all of your application's models. +If you would like to use a custom collection for every model in your application, you should define the `$collectionClass` property on a base model class that is extended by all of your application's models.