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
|
106 |
|
public function __construct() |
36
|
|
|
{ |
37
|
106 |
|
$args = func_get_args(); |
38
|
|
|
|
39
|
106 |
|
$foreignKey = explode('[]->', $args[2]); |
40
|
|
|
|
41
|
106 |
|
$this->path = $foreignKey[0]; |
42
|
106 |
|
$this->key = $foreignKey[1] ?? null; |
43
|
|
|
|
44
|
106 |
|
parent::__construct(...$args); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Hydrate the pivot relationship on the models. |
49
|
|
|
* |
50
|
|
|
* @param \Illuminate\Database\Eloquent\Collection $models |
51
|
|
|
* @param \Illuminate\Database\Eloquent\Model $parent |
52
|
|
|
* @return void |
53
|
|
|
*/ |
54
|
35 |
|
protected function hydratePivotRelation(Collection $models, Model $parent) |
55
|
|
|
{ |
56
|
35 |
|
foreach ($models as $i => $model) { |
57
|
35 |
|
$clone = clone $model; |
58
|
|
|
|
59
|
35 |
|
$models[$i] = $clone->setRelation( |
60
|
35 |
|
$this->getPivotAccessor(), |
61
|
35 |
|
$this->pivotRelation($clone, $parent) |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get the pivot relationship from the query. |
68
|
|
|
* |
69
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
70
|
|
|
* @param \Illuminate\Database\Eloquent\Model $parent |
71
|
|
|
* @return \Illuminate\Database\Eloquent\Model |
72
|
|
|
*/ |
73
|
35 |
|
protected function pivotRelation(Model $model, Model $parent) |
74
|
|
|
{ |
75
|
35 |
|
$attributes = $this->pivotAttributes($model, $parent); |
76
|
|
|
|
77
|
35 |
|
return Pivot::fromAttributes($model, $attributes, null, true); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get the pivot attributes from a model. |
82
|
|
|
* |
83
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
84
|
|
|
* @param \Illuminate\Database\Eloquent\Model $parent |
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
|
|
abstract protected function pivotAttributes(Model $model, Model $parent); |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Execute the query and get the first related model. |
91
|
|
|
* |
92
|
|
|
* @param array $columns |
93
|
|
|
* @return mixed |
94
|
|
|
*/ |
95
|
5 |
|
public function first($columns = ['*']) |
96
|
|
|
{ |
97
|
5 |
|
return $this->take(1)->get($columns)->first(); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get the fully qualified path of the relationship. |
102
|
|
|
* |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
15 |
|
public function getQualifiedPath() |
106
|
|
|
{ |
107
|
15 |
|
return $this->parent->qualifyColumn($this->path); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Get the JSON grammar. |
112
|
|
|
* |
113
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
114
|
|
|
* @return \Staudenmeir\EloquentJsonRelations\Grammars\JsonGrammar |
115
|
|
|
*/ |
116
|
20 |
|
protected function getJsonGrammar(Builder $query) |
117
|
|
|
{ |
118
|
20 |
|
$driver = $query->getConnection()->getDriverName(); |
119
|
|
|
|
120
|
20 |
|
switch ($driver) { |
121
|
20 |
|
case 'mysql': |
122
|
8 |
|
return $query->getConnection()->withTablePrefix(new MySqlGrammar()); |
|
|
|
|
123
|
12 |
|
case 'pgsql': |
124
|
8 |
|
return $query->getConnection()->withTablePrefix(new PostgresGrammar()); |
|
|
|
|
125
|
4 |
|
case 'sqlsrv': |
126
|
4 |
|
return $query->getConnection()->withTablePrefix(new SqlServerGrammar()); |
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
throw new RuntimeException('This database is not supported.'); // @codeCoverageIgnore |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Get the name of the pivot accessor for this relationship. |
134
|
|
|
* |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
35 |
|
public function getPivotAccessor(): string |
138
|
|
|
{ |
139
|
35 |
|
return 'pivot'; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|