Skip to content

Commit d377d47

Browse files
author
Ardalan
committed
improvement
1 parent 5e9cf13 commit d377d47

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,12 @@ class User {
9090

9191
// add eager constraints
9292
function ($relation, $models) {
93-
$relation->getQuery()->whereIn('role_user.user_id', $relation->getKeys($models));
93+
$relation->getQuery()->whereIn(
94+
'role_user.user_id',
95+
collect($models)->map(function ($value) {
96+
return $value->getKey();
97+
})->values()->unique()->sort()->all()
98+
);
9499
}
95100
);
96101
}
@@ -125,7 +130,12 @@ class Permission {
125130

126131
// eager constraints
127132
function ($relation, $models) {
128-
$relation->getQuery()->whereIn('permission_role.permission_id', $relation->getKeys($models));
133+
$relation->getQuery()->whereIn(
134+
'permission_role.permission_id',
135+
collect($models)->map(function ($value) {
136+
return $value->getKey();
137+
})->values()->unique()->sort()->all()
138+
);
129139
}
130140
);
131141
}

src/HasCustomRelations.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@ public function custom(
3232
Closure $baseConstraints,
3333
Closure $eagerConstraints,
3434
array $eagerParentRelations = null,
35-
string $localKey = null
35+
string $localKey = null,
36+
string $foreignKey = null
3637
) {
3738
$instance = $this->newRelatedInstance($related);
3839
$query = $instance->newQuery();
3940

40-
return new Custom($query, $this, $baseConstraints, $eagerConstraints, $eagerParentRelations, $localKey);
41+
return new Custom($query, $this, $baseConstraints, $eagerConstraints, $eagerParentRelations, $localKey, $foreignKey);
4142
}
4243
}

src/Relations/Custom.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,16 @@ public function __construct(
3838
Closure $baseConstraints,
3939
Closure $eagerConstraints,
4040
array $eagerParentRelations = null,
41-
string $localKey = null
41+
string $localKey = null,
42+
string $foreignKey = null,
4243
) {
4344
$this->baseConstraints = $baseConstraints;
4445
$this->eagerConstraints = $eagerConstraints;
4546

4647
if ($eagerParentRelations != null) $parent->setEagerLoads($eagerParentRelations);
4748

4849
$this->localKey = $localKey;
50+
$this->foreignKey = $foreignKey;
4951

5052
parent::__construct($query, $parent);
5153
}
@@ -118,7 +120,12 @@ protected function buildDictionary(Collection $results, $models) {
118120
// parents without having a possibly slow inner loops for every models.
119121
$dictionary = [];
120122

121-
foreach ($models as $model) $dictionary[$model->getKey()] = $results->where($this->localKey ?: $model->getKeyName(), $model->getKey())->all();
123+
foreach ($models as $model) {
124+
$dictionary[$model->getKey()] = $results->where(
125+
$this->localKey ?: $model->getKeyName(),
126+
$this->foreignKey ? $model->getAttribute($this->foreignKey) : $model->getKey()
127+
)->all();
128+
}
122129

123130
return $dictionary;
124131
}

0 commit comments

Comments
 (0)