|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Staudenmeir\EloquentJsonRelations; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo; |
|
8
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany; |
|
9
|
|
|
use Illuminate\Database\Eloquent\Relations\HasManyThrough; |
|
10
|
|
|
use Illuminate\Database\Eloquent\Relations\HasOne; |
|
11
|
|
|
use Illuminate\Database\Eloquent\Relations\HasOneThrough; |
|
12
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany; |
|
13
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphOne; |
|
14
|
|
|
use Illuminate\Support\Str; |
|
15
|
|
|
use Staudenmeir\EloquentJsonRelations\Relations\BelongsToJson; |
|
16
|
|
|
use Staudenmeir\EloquentJsonRelations\Relations\HasManyJson; |
|
17
|
|
|
use Staudenmeir\EloquentJsonRelations\Relations\Postgres\BelongsTo as BelongsToPostgres; |
|
18
|
|
|
use Staudenmeir\EloquentJsonRelations\Relations\Postgres\HasMany as HasManyPostgres; |
|
19
|
|
|
use Staudenmeir\EloquentJsonRelations\Relations\Postgres\HasManyThrough as HasManyThroughPostgres; |
|
20
|
|
|
use Staudenmeir\EloquentJsonRelations\Relations\Postgres\HasOne as HasOnePostgres; |
|
21
|
|
|
use Staudenmeir\EloquentJsonRelations\Relations\Postgres\HasOneThrough as HasOneThroughPostgres; |
|
22
|
|
|
use Staudenmeir\EloquentJsonRelations\Relations\Postgres\MorphMany as MorphManyPostgres; |
|
23
|
|
|
use Staudenmeir\EloquentJsonRelations\Relations\Postgres\MorphOne as MorphOnePostgres; |
|
24
|
|
|
|
|
25
|
|
|
trait HasJsonRelationships |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* Get an attribute from the model. |
|
29
|
|
|
* |
|
30
|
|
|
* @param string $key |
|
31
|
|
|
* @return mixed |
|
32
|
|
|
*/ |
|
33
|
284 |
|
public function getAttribute($key) |
|
34
|
|
|
{ |
|
35
|
284 |
|
$attribute = preg_split('/(->|\[\])/', $key)[0]; |
|
36
|
|
|
|
|
37
|
284 |
|
if (array_key_exists($attribute, $this->attributes)) { |
|
38
|
278 |
|
return $this->getAttributeValue($key); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
214 |
|
return parent::getAttribute($key); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Get a plain attribute (not a relationship). |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $key |
|
48
|
|
|
* @return mixed |
|
49
|
|
|
*/ |
|
50
|
278 |
|
public function getAttributeValue($key) |
|
51
|
|
|
{ |
|
52
|
278 |
|
if (Str::contains($key, '->')) { |
|
53
|
155 |
|
[$key, $path] = explode('->', $key, 2); |
|
54
|
|
|
|
|
55
|
155 |
|
if (substr($key, -2) === '[]') { |
|
56
|
3 |
|
$key = substr($key, 0, -2); |
|
57
|
|
|
|
|
58
|
3 |
|
$path = '*.'.$path; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
155 |
|
$path = str_replace(['->', '[]'], ['.', '.*'], $path); |
|
62
|
|
|
|
|
63
|
155 |
|
return data_get($this->getAttributeValue($key), $path); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
278 |
|
return parent::getAttributeValue($key); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Instantiate a new HasOne relationship. |
|
71
|
|
|
* |
|
72
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
|
73
|
|
|
* @param \Illuminate\Database\Eloquent\Model $parent |
|
74
|
|
|
* @param string $foreignKey |
|
75
|
|
|
* @param string $localKey |
|
76
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasOne |
|
77
|
|
|
*/ |
|
78
|
24 |
|
protected function newHasOne(Builder $query, Model $parent, $foreignKey, $localKey) |
|
79
|
|
|
{ |
|
80
|
24 |
|
if ($query->getConnection()->getDriverName() === 'pgsql') { |
|
81
|
6 |
|
return new HasOnePostgres($query, $parent, $foreignKey, $localKey); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
18 |
|
return new HasOne($query, $parent, $foreignKey, $localKey); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Instantiate a new HasOneThrough relationship. |
|
89
|
|
|
* |
|
90
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
|
91
|
|
|
* @param \Illuminate\Database\Eloquent\Model $farParent |
|
92
|
|
|
* @param \Illuminate\Database\Eloquent\Model $throughParent |
|
93
|
|
|
* @param string $firstKey |
|
94
|
|
|
* @param string $secondKey |
|
95
|
|
|
* @param string $localKey |
|
96
|
|
|
* @param string $secondLocalKey |
|
97
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasOneThrough |
|
98
|
|
|
*/ |
|
99
|
24 |
|
protected function newHasOneThrough(Builder $query, Model $farParent, Model $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey) |
|
100
|
|
|
{ |
|
101
|
24 |
|
if ($query->getConnection()->getDriverName() === 'pgsql') { |
|
102
|
6 |
|
return new HasOneThroughPostgres($query, $farParent, $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
18 |
|
return new HasOneThrough($query, $farParent, $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Instantiate a new MorphOne relationship. |
|
110
|
|
|
* |
|
111
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
|
112
|
|
|
* @param \Illuminate\Database\Eloquent\Model $parent |
|
113
|
|
|
* @param string $type |
|
114
|
|
|
* @param string $id |
|
115
|
|
|
* @param string $localKey |
|
116
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphOne |
|
117
|
|
|
*/ |
|
118
|
20 |
|
protected function newMorphOne(Builder $query, Model $parent, $type, $id, $localKey) |
|
119
|
|
|
{ |
|
120
|
20 |
|
if ($query->getConnection()->getDriverName() === 'pgsql') { |
|
121
|
5 |
|
return new MorphOnePostgres($query, $parent, $type, $id, $localKey); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
15 |
|
return new MorphOne($query, $parent, $type, $id, $localKey); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Instantiate a new BelongsTo relationship. |
|
129
|
|
|
* |
|
130
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
|
131
|
|
|
* @param \Illuminate\Database\Eloquent\Model $child |
|
132
|
|
|
* @param string $foreignKey |
|
133
|
|
|
* @param string $ownerKey |
|
134
|
|
|
* @param string $relation |
|
135
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
|
136
|
|
|
*/ |
|
137
|
36 |
|
protected function newBelongsTo(Builder $query, Model $child, $foreignKey, $ownerKey, $relation) |
|
138
|
|
|
{ |
|
139
|
36 |
|
if ($query->getConnection()->getDriverName() === 'pgsql') { |
|
140
|
9 |
|
return new BelongsToPostgres($query, $child, $foreignKey, $ownerKey, $relation); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
27 |
|
return new BelongsTo($query, $child, $foreignKey, $ownerKey, $relation); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Instantiate a new HasMany relationship. |
|
148
|
|
|
* |
|
149
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
|
150
|
|
|
* @param \Illuminate\Database\Eloquent\Model $parent |
|
151
|
|
|
* @param string $foreignKey |
|
152
|
|
|
* @param string $localKey |
|
153
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
|
154
|
|
|
*/ |
|
155
|
24 |
|
protected function newHasMany(Builder $query, Model $parent, $foreignKey, $localKey) |
|
156
|
|
|
{ |
|
157
|
24 |
|
if ($query->getConnection()->getDriverName() === 'pgsql') { |
|
158
|
6 |
|
return new HasManyPostgres($query, $parent, $foreignKey, $localKey); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
18 |
|
return new HasMany($query, $parent, $foreignKey, $localKey); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Instantiate a new HasManyThrough relationship. |
|
166
|
|
|
* |
|
167
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
|
168
|
|
|
* @param \Illuminate\Database\Eloquent\Model $farParent |
|
169
|
|
|
* @param \Illuminate\Database\Eloquent\Model $throughParent |
|
170
|
|
|
* @param string $firstKey |
|
171
|
|
|
* @param string $secondKey |
|
172
|
|
|
* @param string $localKey |
|
173
|
|
|
* @param string $secondLocalKey |
|
174
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasManyThrough |
|
175
|
|
|
*/ |
|
176
|
24 |
|
protected function newHasManyThrough(Builder $query, Model $farParent, Model $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey) |
|
177
|
|
|
{ |
|
178
|
24 |
|
if ($query->getConnection()->getDriverName() === 'pgsql') { |
|
179
|
6 |
|
return new HasManyThroughPostgres($query, $farParent, $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
18 |
|
return new HasManyThrough($query, $farParent, $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Instantiate a new MorphMany relationship. |
|
187
|
|
|
* |
|
188
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
|
189
|
|
|
* @param \Illuminate\Database\Eloquent\Model $parent |
|
190
|
|
|
* @param string $type |
|
191
|
|
|
* @param string $id |
|
192
|
|
|
* @param string $localKey |
|
193
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
|
194
|
|
|
*/ |
|
195
|
20 |
|
protected function newMorphMany(Builder $query, Model $parent, $type, $id, $localKey) |
|
196
|
|
|
{ |
|
197
|
20 |
|
if ($query->getConnection()->getDriverName() === 'pgsql') { |
|
198
|
5 |
|
return new MorphManyPostgres($query, $parent, $type, $id, $localKey); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
15 |
|
return new MorphMany($query, $parent, $type, $id, $localKey); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Define an inverse one-to-one or many JSON relationship. |
|
206
|
|
|
* |
|
207
|
|
|
* @param string $related |
|
208
|
|
|
* @param string $foreignKey |
|
209
|
|
|
* @param string $ownerKey |
|
210
|
|
|
* @param string $relation |
|
211
|
|
|
* @return \Staudenmeir\EloquentJsonRelations\Relations\BelongsToJson |
|
212
|
|
|
*/ |
|
213
|
70 |
|
public function belongsToJson($related, $foreignKey, $ownerKey = null, $relation = null) |
|
214
|
|
|
{ |
|
215
|
70 |
|
if (is_null($relation)) { |
|
216
|
70 |
|
$relation = $this->guessBelongsToRelation(); |
|
|
|
|
|
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** @var \Illuminate\Database\Eloquent\Model $instance */ |
|
220
|
70 |
|
$instance = $this->newRelatedInstance($related); |
|
|
|
|
|
|
221
|
|
|
|
|
222
|
70 |
|
$ownerKey = $ownerKey ?: $instance->getKeyName(); |
|
223
|
|
|
|
|
224
|
70 |
|
return $this->newBelongsToJson( |
|
225
|
70 |
|
$instance->newQuery(), |
|
226
|
|
|
$this, |
|
|
|
|
|
|
227
|
|
|
$foreignKey, |
|
228
|
|
|
$ownerKey, |
|
229
|
|
|
$relation |
|
230
|
|
|
); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* Instantiate a new BelongsToJson relationship. |
|
235
|
|
|
* |
|
236
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
|
237
|
|
|
* @param \Illuminate\Database\Eloquent\Model $child |
|
238
|
|
|
* @param string $foreignKey |
|
239
|
|
|
* @param string $ownerKey |
|
240
|
|
|
* @param string $relation |
|
241
|
|
|
* @return \Staudenmeir\EloquentJsonRelations\Relations\BelongsToJson |
|
242
|
|
|
*/ |
|
243
|
70 |
|
protected function newBelongsToJson(Builder $query, Model $child, $foreignKey, $ownerKey, $relation) |
|
244
|
|
|
{ |
|
245
|
70 |
|
return new BelongsToJson($query, $child, $foreignKey, $ownerKey, $relation); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* Define a one-to-many JSON relationship. |
|
250
|
|
|
* |
|
251
|
|
|
* @param string $related |
|
252
|
|
|
* @param string $foreignKey |
|
253
|
|
|
* @param string $localKey |
|
254
|
|
|
* @return \Staudenmeir\EloquentJsonRelations\Relations\HasManyJson |
|
255
|
|
|
*/ |
|
256
|
36 |
|
public function hasManyJson($related, $foreignKey, $localKey = null) |
|
257
|
|
|
{ |
|
258
|
|
|
/** @var \Illuminate\Database\Eloquent\Model $instance */ |
|
259
|
36 |
|
$instance = $this->newRelatedInstance($related); |
|
260
|
|
|
|
|
261
|
36 |
|
$localKey = $localKey ?: $this->getKeyName(); |
|
|
|
|
|
|
262
|
|
|
|
|
263
|
36 |
|
return $this->newHasManyJson( |
|
264
|
36 |
|
$instance->newQuery(), |
|
265
|
|
|
$this, |
|
|
|
|
|
|
266
|
36 |
|
$instance->getTable().'.'.$foreignKey, |
|
267
|
|
|
$localKey |
|
268
|
|
|
); |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
/** |
|
272
|
|
|
* Instantiate a new HasManyJson relationship. |
|
273
|
|
|
* |
|
274
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
|
275
|
|
|
* @param \Illuminate\Database\Eloquent\Model $parent |
|
276
|
|
|
* @param string $foreignKey |
|
277
|
|
|
* @param string $localKey |
|
278
|
|
|
* @return \Staudenmeir\EloquentJsonRelations\Relations\HasManyJson |
|
279
|
|
|
*/ |
|
280
|
36 |
|
protected function newHasManyJson(Builder $query, Model $parent, $foreignKey, $localKey) |
|
281
|
|
|
{ |
|
282
|
36 |
|
return new HasManyJson($query, $parent, $foreignKey, $localKey); |
|
283
|
|
|
} |
|
284
|
|
|
} |
|
285
|
|
|
|