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