Skip to content

Commit

Permalink
Merge pull request #2 from jasonlewis/eager-load-constraints
Browse files Browse the repository at this point in the history
Allow eager load constraints to still apply for pivot relations.
  • Loading branch information
ajcastro committed May 29, 2019
2 parents 4478d82 + 4fbcb8b commit c837a52
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
}
],
"require": {
"php": ">=5.4.0"
"php": ">=5.6.0"
},
"autoload": {
"psr-4": {
Expand Down
14 changes: 9 additions & 5 deletions src/EagerLoadPivotBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,15 @@ protected function eagerLoadPivotRelations($models, $pivotAccessor)
*/
protected function getPivotEagerLoadRelations($pivotAccessor)
{
$relations = array_filter(array_keys($this->eagerLoad), function ($relation) use ($pivotAccessor) {
$relations = array_filter($this->eagerLoad, function ($relation) use ($pivotAccessor) {
return $relation != $pivotAccessor && str_contains($relation, $pivotAccessor);
});
return array_map(function ($relation) use ($pivotAccessor) {
return substr($relation, strlen("{$pivotAccessor}."));
}, $relations);
}, ARRAY_FILTER_USE_KEY);

return array_combine(
array_map(function ($relation) use ($pivotAccessor) {
return substr($relation, strlen("{$pivotAccessor}."));
}, array_keys($relations)),
array_values($relations)
);
}
}

0 comments on commit c837a52

Please # to comment.