Skip to content

Commit 9d4e8e6

Browse files
committed
feat: add HasCollection trait
1 parent 0c1796c commit 9d4e8e6

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Illuminate\Database\Eloquent;
4+
5+
/**
6+
* @template TCollection of \Illuminate\Database\Eloquent\Collection
7+
*/
8+
trait HasCollection
9+
{
10+
/**
11+
* Create a new Eloquent Collection instance.
12+
*
13+
* @param array<array-key, \Illuminate\Database\Eloquent\Model> $models
14+
* @return TCollection
15+
*/
16+
public function newCollection(array $models = [])
17+
{
18+
return parent::newCollection($models);
19+
}
20+
}

src/Illuminate/Database/Eloquent/Model.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,13 @@ abstract class Model implements Arrayable, ArrayAccess, CanBeEscapedWhenCastToSt
218218
*/
219219
protected static string $builder = Builder::class;
220220

221+
/**
222+
* The Eloquent collection class to use for the model.
223+
*
224+
* @var class-string<\Illuminate\Database\Eloquent\Collection<*, *>>
225+
*/
226+
protected static string $collection = Collection::class;
227+
221228
/**
222229
* The name of the "created at" column.
223230
*
@@ -1598,7 +1605,7 @@ protected function newBaseQueryBuilder()
15981605
*/
15991606
public function newCollection(array $models = [])
16001607
{
1601-
return new Collection($models);
1608+
return new static::$collection($models);
16021609
}
16031610

16041611
/**

types/Database/Eloquent/Model.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Illuminate\Types\Model;
44

55
use Illuminate\Database\Eloquent\Collection;
6+
use Illuminate\Database\Eloquent\HasCollection;
67
use Illuminate\Database\Eloquent\Model;
78
use User;
89

@@ -32,7 +33,7 @@ function test(User $user, Post $post, Comment $comment): void
3233
assertType('Illuminate\Database\Eloquent\Relations\MorphMany<Illuminate\Notifications\DatabaseNotification, User>', $user->notifications());
3334

3435
assertType('Illuminate\Database\Eloquent\Collection<int, User>', $user->newCollection([new User()]));
35-
assertType('Illuminate\Database\Eloquent\Collection<string, Illuminate\Types\Model\Post>', $post->newCollection(['foo' => new Post()]));
36+
assertType('Illuminate\Types\Model\Posts<(int|string), Illuminate\Types\Model\Post>', $post->newCollection(['foo' => new Post()]));
3637
assertType('Illuminate\Types\Model\Comments', $comment->newCollection([new Comment()]));
3738

3839
assertType('bool', $user->restore());
@@ -41,6 +42,18 @@ function test(User $user, Post $post, Comment $comment): void
4142
}
4243

4344
class Post extends Model
45+
{
46+
/** @use HasCollection<Posts<array-key, static>> */
47+
use HasCollection;
48+
49+
protected static string $collection = Posts::class;
50+
}
51+
52+
/**
53+
* @template TKey of array-key
54+
* @template TModel of Post
55+
* @extends Collection<TKey, TModel> */
56+
class Posts extends Collection
4457
{
4558
}
4659

0 commit comments

Comments
 (0)