Skip to content

Commit

Permalink
fix: EloquentCollection find and unique generics (#53239)
Browse files Browse the repository at this point in the history
  • Loading branch information
calebdw authored Oct 21, 2024
1 parent 2044605 commit 6247370
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/Illuminate/Database/Eloquent/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Collection extends BaseCollection implements QueueableCollection
*
* @param mixed $key
* @param TFindDefault $default
* @return static<TKey, TModel>|TModel|TFindDefault
* @return ($key is (\Illuminate\Contracts\Support\Arrayable<array-key, mixed>|array<mixed>) ? static : TModel|TFindDefault)
*/
public function find($key, $default = null)
{
Expand Down Expand Up @@ -470,7 +470,7 @@ public function intersect($items)
*
* @param (callable(TModel, TKey): mixed)|string|null $key
* @param bool $strict
* @return static<int, TModel>
* @return static
*/
public function unique($key = null, $strict = false)
{
Expand All @@ -485,7 +485,7 @@ public function unique($key = null, $strict = false)
* Returns only the models from the collection with the specified keys.
*
* @param array<array-key, mixed>|null $keys
* @return static<int, TModel>
* @return static
*/
public function only($keys)
{
Expand All @@ -502,7 +502,7 @@ public function only($keys)
* Returns all models in the collection except the models with specified keys.
*
* @param array<array-key, mixed>|null $keys
* @return static<int, TModel>
* @return static
*/
public function except($keys)
{
Expand Down
8 changes: 6 additions & 2 deletions types/Database/Eloquent/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
$collection = User::all();
assertType('Illuminate\Database\Eloquent\Collection<int, User>', $collection);

assertType('Illuminate\Database\Eloquent\Collection<int, User>|User|null', $collection->find(1));
assertType('Illuminate\Database\Eloquent\Collection<int, User>|string|User', $collection->find(1, 'string'));
assertType('User|null', $collection->find(1));
assertType('string|User', $collection->find(1, 'string'));
assertType('Illuminate\Database\Eloquent\Collection<int, User>', $collection->find([1]));

assertType('Illuminate\Database\Eloquent\Collection<int, User>', $collection->load('string'));
assertType('Illuminate\Database\Eloquent\Collection<int, User>', $collection->load(['string']));
Expand Down Expand Up @@ -165,6 +166,9 @@
assertType('Illuminate\Database\Eloquent\Collection<int, User>', $collection->append('string'));
assertType('Illuminate\Database\Eloquent\Collection<int, User>', $collection->append(['string']));

assertType('Illuminate\Database\Eloquent\Collection<int, User>', $collection->unique());
assertType('Illuminate\Database\Eloquent\Collection<int, User>', $collection->uniqueStrict());

assertType('array<User>', $collection->getDictionary());
assertType('array<User>', $collection->getDictionary($collection));
assertType('array<User>', $collection->getDictionary([new User]));
Expand Down

0 comments on commit 6247370

Please sign in to comment.