Skip to content

Commit 68a6a2b

Browse files
author
Michael Deck
committed
Override the getEagerModelKeys method of BelongsTo relation to prevent empty strings from getting passed to FM
1 parent 61a19df commit 68a6a2b

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/Database/Eloquent/Relations/BelongsTo.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,30 @@ public function addEagerConstraints(array $models)
3838

3939
$this->query->{$whereIn}($key, $this->getEagerModelKeys($models));
4040
}
41+
42+
/**
43+
* Gather the keys from an array of related models.
44+
*
45+
* @param array $models
46+
* @return array
47+
*/
48+
protected function getEagerModelKeys(array $models)
49+
{
50+
$keys = [];
51+
52+
// First we need to gather all of the keys from the parent models so we know what
53+
// to query for via the eager loading query. We will add them to an array then
54+
// execute a "where in" statement to gather up all of those related records.
55+
// Unlike other DB Engines FM does not support nulls and uses an empty string instead.
56+
foreach ($models as $model) {
57+
$value = $model->{$this->foreignKey};
58+
if (! is_null($value) && $value !== '') {
59+
$keys[] = $value;
60+
}
61+
}
62+
63+
sort($keys);
64+
65+
return array_values(array_unique($keys));
66+
}
4167
}

0 commit comments

Comments
 (0)