File tree Expand file tree Collapse file tree 2 files changed +20
-3
lines changed
src/Illuminate/Database/Eloquent/Relations Expand file tree Collapse file tree 2 files changed +20
-3
lines changed Original file line number Diff line number Diff line change @@ -116,12 +116,16 @@ public function addEagerConstraints(array $models)
116116 */
117117 protected function getEagerModelKeys (array $ models )
118118 {
119+ $ keys = [];
120+
119121 // First we need to gather all of the keys from the parent models so we know what
120122 // to query for via the eager loading query. We will add them to an array then
121123 // execute a "where in" statement to gather up all of those related records.
122- $ keys = collect ($ models )->map (function ($ model ) {
123- return $ model ->{$ this ->foreignKey };
124- })->filter ()->all ();
124+ foreach ($ models as $ model ) {
125+ if (! is_null ($ value = $ model ->{$ this ->foreignKey })) {
126+ $ keys [] = $ value ;
127+ }
128+ }
125129
126130 // If there are no keys that were not null we will just return an array with either
127131 // null or 0 in (depending on if incrementing keys are in use) so the query wont
Original file line number Diff line number Diff line change @@ -33,6 +33,14 @@ public function testEagerConstraintsAreProperlyAdded()
3333 $ relation ->addEagerConstraints ($ models );
3434 }
3535
36+ public function testIdsInEagerConstraintsCanBeZero ()
37+ {
38+ $ relation = $ this ->getRelation ();
39+ $ relation ->getQuery ()->shouldReceive ('whereIn ' )->once ()->with ('relation.id ' , ['foreign.value ' , 0 ]);
40+ $ models = [new EloquentBelongsToModelStub , new EloquentBelongsToModelStubWithZeroId ];
41+ $ relation ->addEagerConstraints ($ models );
42+ }
43+
3644 public function testRelationIsProperlyInitialized ()
3745 {
3846 $ relation = $ this ->getRelation ();
@@ -143,6 +151,11 @@ class AnotherEloquentBelongsToModelStub extends \Illuminate\Database\Eloquent\Mo
143151 public $ foreign_key = 'foreign.value.two ' ;
144152}
145153
154+ class EloquentBelongsToModelStubWithZeroId extends \Illuminate \Database \Eloquent \Model
155+ {
156+ public $ foreign_key = 0 ;
157+ }
158+
146159class MissingEloquentBelongsToModelStub extends \Illuminate \Database \Eloquent \Model
147160{
148161 public $ foreign_key ;
You can’t perform that action at this time.
0 commit comments