Skip to content

Commit 507f325

Browse files
authored
always alias the Illuminate\Database\Eloquent\Collection (#53735)
while there is no behavior change in this commit, my intent behind this is to help prevent stupid mistakes from assuming what Collection is being referenced. by always aliasing this class explicitly, we gain a little confidence when in the code about what version we are referencing. our use of `Illuminate\Support\Collection` far outweights our use of `Illuminate\Database\Eloquent\Collection`. however, it's very common for both to be used in a file that uses `Illuminate\Database\Eloquent\Collection`. therefore, I say we treat the base Collection as the default, and our Eloquent Collection as our alias ALL the time. this will provide consistency and help avoid stupid mistakes. it is also helpful when doing global searches because now we have a unique token to search for. this idea came to me as I was working on #53726 because making sure I was referencing the correct Collection was the gotcha I had to pay the closest attention to.
1 parent 2176406 commit 507f325

17 files changed

+42
-42
lines changed

src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Closure;
66
use Illuminate\Database\ClassMorphViolationException;
77
use Illuminate\Database\Eloquent\Builder;
8-
use Illuminate\Database\Eloquent\Collection;
8+
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
99
use Illuminate\Database\Eloquent\Model;
1010
use Illuminate\Database\Eloquent\PendingHasThroughRelationship;
1111
use Illuminate\Database\Eloquent\Relations\BelongsTo;
@@ -804,7 +804,7 @@ public function touchOwners()
804804
$this->$relation->fireModelEvent('saved', false);
805805

806806
$this->$relation->touchOwners();
807-
} elseif ($this->$relation instanceof Collection) {
807+
} elseif ($this->$relation instanceof EloquentCollection) {
808808
$this->$relation->each->touchOwners();
809809
}
810810
}

src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use BadMethodCallException;
66
use Closure;
77
use Illuminate\Database\Eloquent\Builder;
8-
use Illuminate\Database\Eloquent\Collection;
8+
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
99
use Illuminate\Database\Eloquent\RelationNotFoundException;
1010
use Illuminate\Database\Eloquent\Relations\BelongsTo;
1111
use Illuminate\Database\Eloquent\Relations\MorphTo;
@@ -550,7 +550,7 @@ public function orWhereNotMorphedTo($relation, $model)
550550
*/
551551
public function whereBelongsTo($related, $relationshipName = null, $boolean = 'and')
552552
{
553-
if (! $related instanceof Collection) {
553+
if (! $related instanceof EloquentCollection) {
554554
$relatedCollection = $related->newCollection([$related]);
555555
} else {
556556
$relatedCollection = $related;

src/Illuminate/Database/Eloquent/Relations/BelongsTo.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Illuminate\Database\Eloquent\Relations;
44

55
use Illuminate\Database\Eloquent\Builder;
6-
use Illuminate\Database\Eloquent\Collection;
6+
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
77
use Illuminate\Database\Eloquent\Model;
88
use Illuminate\Database\Eloquent\Relations\Concerns\ComparesRelatedModels;
99
use Illuminate\Database\Eloquent\Relations\Concerns\InteractsWithDictionary;
@@ -150,7 +150,7 @@ public function initRelation(array $models, $relation)
150150
}
151151

152152
/** @inheritDoc */
153-
public function match(array $models, Collection $results, $relation)
153+
public function match(array $models, EloquentCollection $results, $relation)
154154
{
155155
// First we will get to build a dictionary of the child models by their primary
156156
// key of the relationship, then we can easily match the children back onto

src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Closure;
66
use Illuminate\Contracts\Support\Arrayable;
77
use Illuminate\Database\Eloquent\Builder;
8-
use Illuminate\Database\Eloquent\Collection;
8+
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
99
use Illuminate\Database\Eloquent\Model;
1010
use Illuminate\Database\Eloquent\ModelNotFoundException;
1111
use Illuminate\Database\Eloquent\Relations\Concerns\AsPivot;
@@ -270,7 +270,7 @@ public function initRelation(array $models, $relation)
270270
}
271271

272272
/** @inheritDoc */
273-
public function match(array $models, Collection $results, $relation)
273+
public function match(array $models, EloquentCollection $results, $relation)
274274
{
275275
$dictionary = $this->buildDictionary($results);
276276

@@ -296,7 +296,7 @@ public function match(array $models, Collection $results, $relation)
296296
* @param \Illuminate\Database\Eloquent\Collection<int, TRelatedModel> $results
297297
* @return array<array<string, TRelatedModel>>
298298
*/
299-
protected function buildDictionary(Collection $results)
299+
protected function buildDictionary(EloquentCollection $results)
300300
{
301301
// First we'll build a dictionary of child models keyed by the foreign key
302302
// of the relation so that we will easily and quickly match them to the

src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Illuminate\Database\Eloquent\Relations\Concerns;
44

55
use BackedEnum;
6-
use Illuminate\Database\Eloquent\Collection;
6+
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
77
use Illuminate\Database\Eloquent\Model;
88
use Illuminate\Database\Eloquent\Relations\Pivot;
99
use Illuminate\Support\Collection as BaseCollection;
@@ -611,7 +611,7 @@ protected function parseIds($value)
611611
return [$value->{$this->relatedKey}];
612612
}
613613

614-
if ($value instanceof Collection) {
614+
if ($value instanceof EloquentCollection) {
615615
return $value->pluck($this->relatedKey)->all();
616616
}
617617

src/Illuminate/Database/Eloquent/Relations/HasMany.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Illuminate\Database\Eloquent\Relations;
44

5-
use Illuminate\Database\Eloquent\Collection;
5+
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
66

77
/**
88
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
@@ -53,7 +53,7 @@ public function initRelation(array $models, $relation)
5353
}
5454

5555
/** @inheritDoc */
56-
public function match(array $models, Collection $results, $relation)
56+
public function match(array $models, EloquentCollection $results, $relation)
5757
{
5858
return $this->matchMany($models, $results, $relation);
5959
}

src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Illuminate\Database\Eloquent\Relations;
44

55
use Illuminate\Database\Eloquent\Builder;
6-
use Illuminate\Database\Eloquent\Collection;
6+
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
77
use Illuminate\Database\Eloquent\Relations\Concerns\InteractsWithDictionary;
88

99
/**
@@ -46,7 +46,7 @@ public function initRelation(array $models, $relation)
4646
}
4747

4848
/** @inheritDoc */
49-
public function match(array $models, Collection $results, $relation)
49+
public function match(array $models, EloquentCollection $results, $relation)
5050
{
5151
$dictionary = $this->buildDictionary($results);
5252

src/Illuminate/Database/Eloquent/Relations/HasOne.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Illuminate\Contracts\Database\Eloquent\SupportsPartialRelations;
66
use Illuminate\Database\Eloquent\Builder;
7-
use Illuminate\Database\Eloquent\Collection;
7+
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
88
use Illuminate\Database\Eloquent\Model;
99
use Illuminate\Database\Eloquent\Relations\Concerns\CanBeOneOfMany;
1010
use Illuminate\Database\Eloquent\Relations\Concerns\ComparesRelatedModels;
@@ -42,7 +42,7 @@ public function initRelation(array $models, $relation)
4242
}
4343

4444
/** @inheritDoc */
45-
public function match(array $models, Collection $results, $relation)
45+
public function match(array $models, EloquentCollection $results, $relation)
4646
{
4747
return $this->matchOne($models, $results, $relation);
4848
}

src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Illuminate\Database\Eloquent\Relations;
44

55
use Illuminate\Database\Eloquent\Builder;
6-
use Illuminate\Database\Eloquent\Collection;
6+
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
77
use Illuminate\Database\Eloquent\Model;
88
use Illuminate\Database\Eloquent\Relations\Concerns\InteractsWithDictionary;
99
use Illuminate\Database\Eloquent\Relations\Concerns\SupportsInverseRelations;
@@ -119,7 +119,7 @@ public function addEagerConstraints(array $models)
119119
* @param string $relation
120120
* @return array<int, TDeclaringModel>
121121
*/
122-
public function matchOne(array $models, Collection $results, $relation)
122+
public function matchOne(array $models, EloquentCollection $results, $relation)
123123
{
124124
return $this->matchOneOrMany($models, $results, $relation, 'one');
125125
}
@@ -132,7 +132,7 @@ public function matchOne(array $models, Collection $results, $relation)
132132
* @param string $relation
133133
* @return array<int, TDeclaringModel>
134134
*/
135-
public function matchMany(array $models, Collection $results, $relation)
135+
public function matchMany(array $models, EloquentCollection $results, $relation)
136136
{
137137
return $this->matchOneOrMany($models, $results, $relation, 'many');
138138
}
@@ -146,7 +146,7 @@ public function matchMany(array $models, Collection $results, $relation)
146146
* @param string $type
147147
* @return array<int, TDeclaringModel>
148148
*/
149-
protected function matchOneOrMany(array $models, Collection $results, $relation, $type)
149+
protected function matchOneOrMany(array $models, EloquentCollection $results, $relation, $type)
150150
{
151151
$dictionary = $this->buildDictionary($results);
152152

@@ -189,7 +189,7 @@ protected function getRelationValue(array $dictionary, $key, $type)
189189
* @param \Illuminate\Database\Eloquent\Collection<int, TRelatedModel> $results
190190
* @return array<array<int, TRelatedModel>>
191191
*/
192-
protected function buildDictionary(Collection $results)
192+
protected function buildDictionary(EloquentCollection $results)
193193
{
194194
$foreign = $this->getForeignKeyName();
195195

src/Illuminate/Database/Eloquent/Relations/HasOneOrManyThrough.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Closure;
66
use Illuminate\Contracts\Support\Arrayable;
77
use Illuminate\Database\Eloquent\Builder;
8-
use Illuminate\Database\Eloquent\Collection;
8+
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
99
use Illuminate\Database\Eloquent\Model;
1010
use Illuminate\Database\Eloquent\ModelNotFoundException;
1111
use Illuminate\Database\Eloquent\Relations\Concerns\InteractsWithDictionary;
@@ -178,7 +178,7 @@ public function addEagerConstraints(array $models)
178178
* @param \Illuminate\Database\Eloquent\Collection<int, TRelatedModel> $results
179179
* @return array<array<TRelatedModel>>
180180
*/
181-
protected function buildDictionary(Collection $results)
181+
protected function buildDictionary(EloquentCollection $results)
182182
{
183183
$dictionary = [];
184184

src/Illuminate/Database/Eloquent/Relations/HasOneThrough.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Illuminate\Database\Eloquent\Relations;
44

5-
use Illuminate\Database\Eloquent\Collection;
5+
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
66
use Illuminate\Database\Eloquent\Model;
77
use Illuminate\Database\Eloquent\Relations\Concerns\InteractsWithDictionary;
88
use Illuminate\Database\Eloquent\Relations\Concerns\SupportsDefaultModels;
@@ -35,7 +35,7 @@ public function initRelation(array $models, $relation)
3535
}
3636

3737
/** @inheritDoc */
38-
public function match(array $models, Collection $results, $relation)
38+
public function match(array $models, EloquentCollection $results, $relation)
3939
{
4040
$dictionary = $this->buildDictionary($results);
4141

src/Illuminate/Database/Eloquent/Relations/MorphMany.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Illuminate\Database\Eloquent\Relations;
44

5-
use Illuminate\Database\Eloquent\Collection;
5+
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
66

77
/**
88
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
@@ -54,7 +54,7 @@ public function initRelation(array $models, $relation)
5454
}
5555

5656
/** @inheritDoc */
57-
public function match(array $models, Collection $results, $relation)
57+
public function match(array $models, EloquentCollection $results, $relation)
5858
{
5959
return $this->matchMany($models, $results, $relation);
6060
}

src/Illuminate/Database/Eloquent/Relations/MorphOne.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Illuminate\Contracts\Database\Eloquent\SupportsPartialRelations;
66
use Illuminate\Database\Eloquent\Builder;
7-
use Illuminate\Database\Eloquent\Collection;
7+
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
88
use Illuminate\Database\Eloquent\Model;
99
use Illuminate\Database\Eloquent\Relations\Concerns\CanBeOneOfMany;
1010
use Illuminate\Database\Eloquent\Relations\Concerns\ComparesRelatedModels;
@@ -42,7 +42,7 @@ public function initRelation(array $models, $relation)
4242
}
4343

4444
/** @inheritDoc */
45-
public function match(array $models, Collection $results, $relation)
45+
public function match(array $models, EloquentCollection $results, $relation)
4646
{
4747
return $this->matchOne($models, $results, $relation);
4848
}

src/Illuminate/Database/Eloquent/Relations/MorphTo.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use BadMethodCallException;
66
use Illuminate\Database\Eloquent\Builder;
7-
use Illuminate\Database\Eloquent\Collection;
7+
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
88
use Illuminate\Database\Eloquent\Model;
99
use Illuminate\Database\Eloquent\Relations\Concerns\InteractsWithDictionary;
1010

@@ -96,7 +96,7 @@ public function __construct(Builder $query, Model $parent, $foreignKey, $ownerKe
9696
#[\Override]
9797
public function addEagerConstraints(array $models)
9898
{
99-
$this->buildDictionary($this->models = new Collection($models));
99+
$this->buildDictionary($this->models = new EloquentCollection($models));
100100
}
101101

102102
/**
@@ -105,7 +105,7 @@ public function addEagerConstraints(array $models)
105105
* @param \Illuminate\Database\Eloquent\Collection<int, TRelatedModel> $models
106106
* @return void
107107
*/
108-
protected function buildDictionary(Collection $models)
108+
protected function buildDictionary(EloquentCollection $models)
109109
{
110110
foreach ($models as $model) {
111111
if ($model->{$this->morphType}) {
@@ -201,7 +201,7 @@ public function createModelByType($type)
201201

202202
/** @inheritDoc */
203203
#[\Override]
204-
public function match(array $models, Collection $results, $relation)
204+
public function match(array $models, EloquentCollection $results, $relation)
205205
{
206206
return $models;
207207
}
@@ -213,7 +213,7 @@ public function match(array $models, Collection $results, $relation)
213213
* @param \Illuminate\Database\Eloquent\Collection<int, TRelatedModel> $results
214214
* @return void
215215
*/
216-
protected function matchToMorphParents($type, Collection $results)
216+
protected function matchToMorphParents($type, EloquentCollection $results)
217217
{
218218
foreach ($results as $result) {
219219
$ownerKey = ! is_null($this->ownerKey) ? $this->getDictionaryKey($result->{$this->ownerKey}) : $result->getKey();

src/Illuminate/Database/Eloquent/Relations/Relation.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Closure;
66
use Illuminate\Contracts\Database\Eloquent\Builder as BuilderContract;
77
use Illuminate\Database\Eloquent\Builder;
8-
use Illuminate\Database\Eloquent\Collection;
8+
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
99
use Illuminate\Database\Eloquent\Model;
1010
use Illuminate\Database\Eloquent\ModelNotFoundException;
1111
use Illuminate\Database\MultipleRecordsFoundException;
@@ -152,7 +152,7 @@ abstract public function initRelation(array $models, $relation);
152152
* @param string $relation
153153
* @return array<int, TDeclaringModel>
154154
*/
155-
abstract public function match(array $models, Collection $results, $relation);
155+
abstract public function match(array $models, EloquentCollection $results, $relation);
156156

157157
/**
158158
* Get the results of the relationship.

src/Illuminate/Notifications/DatabaseNotificationCollection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
namespace Illuminate\Notifications;
44

5-
use Illuminate\Database\Eloquent\Collection;
5+
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
66

77
/**
88
* @template TKey of array-key
99
* @template TModel of DatabaseNotification
1010
*
1111
* @extends \Illuminate\Database\Eloquent\Collection<TKey, TModel>
1212
*/
13-
class DatabaseNotificationCollection extends Collection
13+
class DatabaseNotificationCollection extends EloquentCollection
1414
{
1515
/**
1616
* Mark all notifications as read.

src/Illuminate/Testing/TestView.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Illuminate\Testing;
44

55
use Closure;
6-
use Illuminate\Database\Eloquent\Collection;
6+
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
77
use Illuminate\Database\Eloquent\Model;
88
use Illuminate\Support\Arr;
99
use Illuminate\Support\Traits\Macroable;
@@ -61,10 +61,10 @@ public function assertViewHas($key, $value = null)
6161
PHPUnit::assertTrue($value(Arr::get($this->view->gatherData(), $key)));
6262
} elseif ($value instanceof Model) {
6363
PHPUnit::assertTrue($value->is(Arr::get($this->view->gatherData(), $key)));
64-
} elseif ($value instanceof Collection) {
64+
} elseif ($value instanceof EloquentCollection) {
6565
$actual = Arr::get($this->view->gatherData(), $key);
6666

67-
PHPUnit::assertInstanceOf(Collection::class, $actual);
67+
PHPUnit::assertInstanceOf(EloquentCollection::class, $actual);
6868
PHPUnit::assertSameSize($value, $actual);
6969

7070
$value->each(fn ($item, $index) => PHPUnit::assertTrue($actual->get($index)->is($item)));

0 commit comments

Comments
 (0)