1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Staudenmeir\EloquentJsonRelations\Relations\Postgres; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
6
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo as Base; |
7
|
|
|
|
8
|
|
|
class BelongsTo extends Base |
9
|
|
|
{ |
10
|
|
|
use IsPostgresRelation; |
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
|
|
|
if ($parentQuery->getQuery()->from == $query->getQuery()->from) { |
23
|
|
|
return $this->getRelationExistenceQueryForSelfRelation($query, $parentQuery, $columns); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
$first = $this->jsonColumn($query, $this->related, $this->getQualifiedForeignKeyName(), $this->ownerKey); |
27
|
|
|
|
28
|
|
|
return $query->select($columns)->whereColumn( |
|
|
|
|
29
|
|
|
$first, |
30
|
|
|
'=', |
31
|
|
|
$query->qualifyColumn($this->ownerKey) |
32
|
|
|
); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Add the constraints for a relationship query on the same table. |
37
|
|
|
* |
38
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
39
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $parentQuery |
40
|
|
|
* @param array|mixed $columns |
41
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
42
|
|
|
*/ |
43
|
|
|
public function getRelationExistenceQueryForSelfRelation(Builder $query, Builder $parentQuery, $columns = ['*']) |
44
|
|
|
{ |
45
|
|
|
$query->select($columns)->from( |
46
|
|
|
$query->getModel()->getTable().' as '.$hash = $this->getRelationCountHash() |
|
|
|
|
47
|
|
|
); |
48
|
|
|
|
49
|
|
|
$query->getModel()->setTable($hash); |
50
|
|
|
|
51
|
|
|
$first = $this->jsonColumn($query, $this->related, $this->getQualifiedForeignKeyName(), $this->ownerKey); |
52
|
|
|
|
53
|
|
|
return $query->whereColumn( |
|
|
|
|
54
|
|
|
$first, |
55
|
|
|
$hash.'.'.$this->ownerKey |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|