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 IsConcatenableDescendantsRelation |
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
|
42 |
|
public function addEagerConstraintsToDeepRelationship(Builder $query, array $models): void |
21
|
|
|
{ |
22
|
42 |
|
$andSelf = $this->andSelf; |
23
|
|
|
|
24
|
42 |
|
$this->andSelf = true; |
|
|
|
|
25
|
|
|
|
26
|
42 |
|
$this->addEagerConstraints($models); |
|
|
|
|
27
|
|
|
|
28
|
42 |
|
$this->andSelf = $andSelf; |
29
|
|
|
|
30
|
42 |
|
$this->mergeExpressions($query, $this->query); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Match the eagerly loaded results for a deep relationship to their parents. |
35
|
|
|
* |
36
|
|
|
* @param array $models |
37
|
|
|
* @param \Illuminate\Database\Eloquent\Collection $results |
38
|
|
|
* @param string $relation |
39
|
|
|
* @param string $type |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
42 |
|
public function matchResultsForDeepRelationship( |
43
|
|
|
array $models, |
44
|
|
|
Collection $results, |
45
|
|
|
string $relation, |
46
|
|
|
string $type = 'many' |
47
|
|
|
): array { |
48
|
42 |
|
$dictionary = $this->buildDictionaryForDeepRelationship($results); |
49
|
|
|
|
50
|
42 |
|
foreach ($models as $model) { |
51
|
42 |
|
$key = $model->{$this->localKey}; |
52
|
|
|
|
53
|
42 |
|
if (isset($dictionary[$key])) { |
54
|
42 |
|
$value = $dictionary[$key]; |
55
|
|
|
|
56
|
42 |
|
$value = $type === 'one' ? reset($value) : $this->related->newCollection($value); |
57
|
|
|
|
58
|
42 |
|
$model->setRelation($relation, $value); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
42 |
|
return $models; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Build the model dictionary for a deep relation. |
67
|
|
|
* |
68
|
|
|
* @param \Illuminate\Database\Eloquent\Collection $results |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
42 |
|
protected function buildDictionaryForDeepRelationship(Collection $results): array |
72
|
|
|
{ |
73
|
42 |
|
$pathSeparator = $this->related->getPathSeparator(); |
74
|
|
|
|
75
|
42 |
|
if (!$this->andSelf) { |
76
|
18 |
|
$results = $results->filter( |
77
|
18 |
|
fn (Model $result) => str_contains($result->laravel_through_key, $pathSeparator) |
78
|
18 |
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
42 |
|
return $results->mapToDictionary(function (Model $result) use ($pathSeparator) { |
82
|
42 |
|
$key = strtok($result->laravel_through_key, $pathSeparator); |
83
|
|
|
|
84
|
42 |
|
return [$key => $result]; |
85
|
42 |
|
})->all(); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|