1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Traits\Concatenation; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
6
|
|
|
use Illuminate\Database\Eloquent\Collection; |
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
|
9
|
|
|
trait IsConcatenableAncestorsRelation |
10
|
|
|
{ |
11
|
|
|
use IsConcatenableRelation; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Set the constraints for an eager load of the deep relation. |
15
|
|
|
* |
16
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
17
|
|
|
* @param array $models |
18
|
|
|
* @return void |
19
|
|
|
*/ |
20
|
20 |
|
public function addEagerConstraintsToDeepRelationship(Builder $query, array $models): void |
21
|
|
|
{ |
22
|
20 |
|
$this->addEagerConstraints($models); |
|
|
|
|
23
|
|
|
|
24
|
20 |
|
$this->mergeExpressions($query, $this->query); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Match the eagerly loaded results for a deep relationship to their parents. |
29
|
|
|
* |
30
|
|
|
* @param array $models |
31
|
|
|
* @param \Illuminate\Database\Eloquent\Collection $results |
32
|
|
|
* @param string $relation |
33
|
|
|
* @return array |
34
|
|
|
*/ |
35
|
20 |
|
public function matchResultsForDeepRelationship(array $models, Collection $results, string $relation): array |
36
|
|
|
{ |
37
|
20 |
|
$dictionary = $this->buildDictionaryForDeepRelationship($results); |
38
|
|
|
|
39
|
20 |
|
$attribute = $this->andSelf ? $this->localKey : $this->getForeignKeyName(); |
|
|
|
|
40
|
|
|
|
41
|
20 |
|
foreach ($models as $model) { |
42
|
20 |
|
$key = $model->$attribute; |
43
|
|
|
|
44
|
20 |
|
if (isset($dictionary[$key])) { |
45
|
20 |
|
$value = $this->related->newCollection($dictionary[$key]); |
46
|
|
|
|
47
|
20 |
|
$model->setRelation($relation, $value); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
20 |
|
return $models; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Build the model dictionary for a deep relation. |
56
|
|
|
* |
57
|
|
|
* @param \Illuminate\Database\Eloquent\Collection $results |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
20 |
|
protected function buildDictionaryForDeepRelationship(Collection $results): array |
61
|
|
|
{ |
62
|
20 |
|
$pathSeparator = $this->related->getPathSeparator(); |
63
|
|
|
|
64
|
20 |
|
return $results->mapToDictionary(function (Model $result) use ($pathSeparator) { |
65
|
20 |
|
$key = strtok($result->laravel_through_key, $pathSeparator); |
66
|
|
|
|
67
|
20 |
|
return [$key => $result]; |
68
|
20 |
|
})->all(); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.