1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Staudenmeir\EloquentJsonRelations\Relations\Postgres; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
6
|
|
|
|
7
|
|
|
trait HasOneOrMany |
8
|
|
|
{ |
9
|
|
|
use IsPostgresRelation; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Add the constraints for a relationship query. |
13
|
|
|
* |
14
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
15
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $parentQuery |
16
|
|
|
* @param array|mixed $columns |
17
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
18
|
|
|
*/ |
19
|
|
|
public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*']) |
20
|
|
|
{ |
21
|
|
|
if ($query->getQuery()->from == $parentQuery->getQuery()->from) { |
22
|
|
|
return $this->getRelationExistenceQueryForSelfRelation($query, $parentQuery, $columns); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
$second = $this->jsonColumn($query, $this->parent, $this->getExistenceCompareKey(), $this->localKey); |
|
|
|
|
26
|
|
|
|
27
|
|
|
return $query->select($columns)->whereColumn( |
|
|
|
|
28
|
|
|
$this->getQualifiedParentKeyName(), |
|
|
|
|
29
|
|
|
'=', |
30
|
|
|
$second |
|
|
|
|
31
|
|
|
); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Add the constraints for a relationship query on the same table. |
36
|
|
|
* |
37
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
38
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $parentQuery |
39
|
|
|
* @param array|mixed $columns |
40
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
41
|
|
|
*/ |
42
|
|
|
public function getRelationExistenceQueryForSelfRelation(Builder $query, Builder $parentQuery, $columns = ['*']) |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
$query->from($query->getModel()->getTable().' as '.$hash = $this->getRelationCountHash()); |
|
|
|
|
45
|
|
|
|
46
|
|
|
$query->getModel()->setTable($hash); |
47
|
|
|
|
48
|
|
|
$second = $this->jsonColumn($query, $this->parent, $hash.'.'.$this->getForeignKeyName(), $this->localKey); |
|
|
|
|
49
|
|
|
|
50
|
|
|
return $query->select($columns)->whereColumn( |
|
|
|
|
51
|
|
|
$this->getQualifiedParentKeyName(), |
52
|
|
|
'=', |
53
|
|
|
$second |
|
|
|
|
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|