1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Staudenmeir\EloquentJsonRelations\Relations; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
6
|
|
|
use Illuminate\Database\Eloquent\Collection; |
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use Illuminate\Database\Eloquent\Relations\Pivot; |
9
|
|
|
use RuntimeException; |
10
|
|
|
use Staudenmeir\EloquentJsonRelations\Grammars\MySqlGrammar; |
11
|
|
|
use Staudenmeir\EloquentJsonRelations\Grammars\PostgresGrammar; |
12
|
|
|
use Staudenmeir\EloquentJsonRelations\Grammars\SqlServerGrammar; |
13
|
|
|
|
14
|
|
|
trait IsJsonRelation |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* The base path of the foreign key. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $path; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The optional object key of the foreign key. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $key; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Create a new JSON relationship instance. |
32
|
|
|
* |
33
|
|
|
* @return void |
34
|
|
|
*/ |
35
|
|
|
public function __construct() |
36
|
|
|
{ |
37
|
|
|
$args = func_get_args(); |
38
|
|
|
|
39
|
|
|
$foreignKey = explode('[]->', $args[2]); |
40
|
|
|
|
41
|
|
|
$this->path = $foreignKey[0]; |
42
|
|
|
$this->key = $foreignKey[1] ?? null; |
43
|
|
|
|
44
|
|
|
parent::__construct(...$args); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get the results of the relationship. |
49
|
|
|
* |
50
|
|
|
* @return mixed |
51
|
|
|
*/ |
52
|
|
|
public function getResults() |
53
|
|
|
{ |
54
|
|
|
return $this->get(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Execute the query as a "select" statement. |
59
|
|
|
* |
60
|
|
|
* @param array $columns |
61
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
62
|
|
|
*/ |
63
|
|
|
public function get($columns = ['*']) |
64
|
|
|
{ |
65
|
|
|
$models = parent::get($columns); |
66
|
|
|
|
67
|
|
|
if ($this->key) { |
68
|
|
|
$this->hydratePivotRelation($models, $this->parent); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $models; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Hydrate the pivot relationship on the models. |
76
|
|
|
* |
77
|
|
|
* @param \Illuminate\Database\Eloquent\Collection $models |
78
|
|
|
* @param \Illuminate\Database\Eloquent\Model $parent |
79
|
|
|
* @return void |
80
|
|
|
*/ |
81
|
|
|
protected function hydratePivotRelation(Collection $models, Model $parent) |
82
|
|
|
{ |
83
|
|
|
foreach ($models as $i => $model) { |
84
|
|
|
$clone = clone $model; |
85
|
|
|
|
86
|
|
|
$models[$i] = $clone->setRelation('pivot', $this->pivotRelation($clone, $parent)); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get the pivot relationship from the query. |
92
|
|
|
* |
93
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
94
|
|
|
* @param \Illuminate\Database\Eloquent\Model $parent |
95
|
|
|
* @return \Illuminate\Database\Eloquent\Model |
96
|
|
|
*/ |
97
|
|
|
protected function pivotRelation(Model $model, Model $parent) |
98
|
|
|
{ |
99
|
|
|
$attributes = $this->pivotAttributes($model, $parent); |
100
|
|
|
|
101
|
|
|
return Pivot::fromAttributes($model, $attributes, null, true); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get the pivot attributes from a model. |
106
|
|
|
* |
107
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
108
|
|
|
* @param \Illuminate\Database\Eloquent\Model $parent |
109
|
|
|
* @return array |
110
|
|
|
*/ |
111
|
|
|
abstract protected function pivotAttributes(Model $model, Model $parent); |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get the fully qualified path of the relationship. |
115
|
|
|
* |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
|
|
public function getQualifiedPath() |
119
|
|
|
{ |
120
|
|
|
return $this->parent->qualifyColumn($this->path); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Get the JSON grammar. |
125
|
|
|
* |
126
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
127
|
|
|
* @return \Staudenmeir\EloquentJsonRelations\Grammars\JsonGrammar |
128
|
|
|
*/ |
129
|
|
|
protected function getJsonGrammar(Builder $query) { |
130
|
|
|
$driver = $query->getConnection()->getDriverName(); |
131
|
|
|
|
132
|
|
|
switch ($driver) { |
133
|
|
|
case 'mysql': |
134
|
|
|
return new MySqlGrammar; |
135
|
|
|
case 'pgsql': |
136
|
|
|
return new PostgresGrammar; |
137
|
|
|
case 'sqlsrv': |
138
|
|
|
return new SqlServerGrammar; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
throw new RuntimeException('This database is not supported.'); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|