1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Staudenmeir\EloquentHasManyDeep\Eloquent\Relations\Traits; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Exception; |
7
|
|
|
use Illuminate\Database\Eloquent\Builder; |
8
|
|
|
use Staudenmeir\EloquentHasManyDeep\HasTableAlias; |
9
|
|
|
|
10
|
|
|
trait HasExistenceQueries |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Add the constraints for a relationship query. |
14
|
|
|
* |
15
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
16
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $parentQuery |
17
|
|
|
* @param array|mixed $columns |
18
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
19
|
|
|
*/ |
20
|
|
|
public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*']) |
21
|
|
|
{ |
22
|
|
|
$this->setRelationExistenceQueryAlias($parentQuery); |
23
|
|
|
|
24
|
|
|
if ($this->firstKey instanceof Closure || $this->localKey instanceof Closure) { |
25
|
|
|
$this->performJoin($query); |
|
|
|
|
26
|
|
|
|
27
|
|
|
$closureKey = $this->firstKey instanceof Closure ? $this->firstKey : $this->localKey; |
28
|
|
|
|
29
|
|
|
$closureKey($query, $parentQuery); |
30
|
|
|
|
31
|
|
|
return $query->select($columns); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$query = parent::getRelationExistenceQuery($query, $parentQuery, $columns); |
35
|
|
|
|
36
|
|
|
if (is_array($this->foreignKeys[0])) { |
37
|
|
|
$column = $this->throughParent->qualifyColumn($this->foreignKeys[0][0]); |
38
|
|
|
|
39
|
|
|
$query->where($column, '=', $this->farParent->getMorphClass()); |
40
|
|
|
} elseif ($this->hasLeadingCompositeKey()) { |
|
|
|
|
41
|
|
|
$this->getRelationExistenceQueryWithCompositeKey($query); |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $query; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Add the constraints for a relationship query on the same table. |
49
|
|
|
* |
50
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
51
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $parentQuery |
52
|
|
|
* @param array|mixed $columns |
53
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
54
|
|
|
*/ |
55
|
|
|
public function getRelationExistenceQueryForSelfRelation(Builder $query, Builder $parentQuery, $columns = ['*']) |
56
|
|
|
{ |
57
|
|
|
$hash = $this->getRelationCountHash(); |
|
|
|
|
58
|
|
|
|
59
|
|
|
$query->from($query->getModel()->getTable().' as '.$hash); |
60
|
|
|
|
61
|
|
|
$this->performJoin($query); |
62
|
|
|
|
63
|
|
|
$query->getModel()->setTable($hash); |
64
|
|
|
|
65
|
|
|
return $query->select($columns)->whereColumn( |
|
|
|
|
66
|
|
|
$parentQuery->getQuery()->from.'.'.$this->localKey, |
|
|
|
|
67
|
|
|
'=', |
68
|
|
|
$this->getQualifiedFirstKeyName() |
|
|
|
|
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Set the table alias for a relation existence query if necessary. |
74
|
|
|
* |
75
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $parentQuery |
76
|
|
|
* @return void |
77
|
|
|
*/ |
78
|
|
|
protected function setRelationExistenceQueryAlias(Builder $parentQuery): void |
79
|
|
|
{ |
80
|
|
|
foreach ($this->throughParents as $throughParent) { |
81
|
|
|
if ($throughParent->getTable() === $parentQuery->getQuery()->from) { |
82
|
|
|
if (!in_array(HasTableAlias::class, class_uses_recursive($throughParent))) { |
83
|
|
|
$traitClass = HasTableAlias::class; |
84
|
|
|
$parentClass = get_class($throughParent); |
85
|
|
|
|
86
|
|
|
throw new Exception( |
87
|
|
|
<<<EOT |
88
|
|
|
This query requires an additional trait. Please add the $traitClass trait to $parentClass. |
89
|
|
|
See https://github.com/staudenmeir/eloquent-has-many-deep/issues/137 for details. |
90
|
|
|
EOT |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$table = $throughParent->getTable() . ' as ' . $this->getRelationCountHash(); |
95
|
|
|
|
96
|
|
|
$throughParent->setTable($table); |
97
|
|
|
|
98
|
|
|
break; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|