1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Library\CrudPanel\Traits; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Illuminate\Support\Arr; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
|
9
|
|
|
trait Relationships |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* From the field entity we get the relation instance. |
13
|
|
|
* |
14
|
|
|
* @param array $entity |
15
|
|
|
* @return object |
16
|
|
|
*/ |
17
|
|
|
public function getRelationInstance($field) |
18
|
|
|
{ |
19
|
|
|
$entity = $this->getOnlyRelationEntity($field); |
20
|
|
|
$possible_method = Str::before($entity, '.'); |
21
|
|
|
$model = $this->model; |
22
|
|
|
|
23
|
|
|
if (method_exists($model, $possible_method)) { |
24
|
|
|
$parts = explode('.', $entity); |
25
|
|
|
// here we are going to iterate through all relation parts to check |
26
|
|
|
foreach ($parts as $i => $part) { |
27
|
|
|
$relation = $model->$part(); |
28
|
|
|
$model = $relation->getRelated(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
return $relation; |
|
|
|
|
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Get the fields for relationships, according to the relation type. It looks only for direct |
37
|
|
|
* relations - it will NOT look through relationships of relationships. |
38
|
|
|
* |
39
|
|
|
* @param string|array $relation_types Eloquent relation class or array of Eloquent relation classes. Eg: BelongsTo |
40
|
|
|
* |
41
|
|
|
* @return array The fields with corresponding relation types. |
42
|
|
|
*/ |
43
|
|
|
public function getFieldsWithRelationType($relation_types): array |
44
|
|
|
{ |
45
|
|
|
$relation_types = (array) $relation_types; |
46
|
|
|
|
47
|
|
|
return collect($this->fields()) |
|
|
|
|
48
|
|
|
->where('model') |
49
|
|
|
->whereIn('relation_type', $relation_types) |
50
|
|
|
->filter(function ($item) { |
51
|
|
|
$related_model = get_class($this->model->{Str::before($item['entity'], '.')}()->getRelated()); |
52
|
|
|
|
53
|
|
|
return Str::contains($item['entity'], '.') && $item['model'] !== $related_model ? false : true; |
54
|
|
|
}) |
55
|
|
|
->toArray(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Grabs an relation instance and returns the class name of the related model. |
60
|
|
|
* |
61
|
|
|
* @param array $field |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
|
|
public function inferFieldModelFromRelationship($field) |
65
|
|
|
{ |
66
|
|
|
$relation = $this->getRelationInstance($field); |
67
|
|
|
|
68
|
|
|
return get_class($relation->getRelated()); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Return the relation type from a given field: BelongsTo, HasOne ... etc. |
73
|
|
|
* |
74
|
|
|
* @param array $field |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
public function inferRelationTypeFromRelationship($field) |
78
|
|
|
{ |
79
|
|
|
$relation = $this->getRelationInstance($field); |
80
|
|
|
|
81
|
|
|
return Arr::last(explode('\\', get_class($relation))); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Parse the field name back to the related entity after the form is submited. |
86
|
|
|
* Its called in getAllFieldNames(). |
87
|
|
|
* |
88
|
|
|
* @param array $fields |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
|
|
public function parseRelationFieldNamesFromHtml($fields) |
92
|
|
|
{ |
93
|
|
|
foreach ($fields as &$field) { |
94
|
|
|
// we only want to parse fields that has a relation type and their name contains [ ] used in html. |
95
|
|
|
if (isset($field['relation_type']) && preg_match('/[\[\]]/', $field['name']) !== 0) { |
96
|
|
|
$chunks = explode('[', $field['name']); |
97
|
|
|
|
98
|
|
|
foreach ($chunks as &$chunk) { |
99
|
|
|
if (strpos($chunk, ']')) { |
100
|
|
|
$chunk = str_replace(']', '', $chunk); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
$field['name'] = implode('.', $chunks); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $fields; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
protected function changeBelongsToNamesFromRelationshipToForeignKey($data) |
111
|
|
|
{ |
112
|
|
|
$belongs_to_fields = $this->getFieldsWithRelationType('BelongsTo'); |
113
|
|
|
|
114
|
|
|
foreach ($belongs_to_fields as $relation_field) { |
115
|
|
|
$relation = $this->getRelationInstance($relation_field); |
116
|
|
|
if (Arr::has($data, $relation->getRelationName())) { |
117
|
|
|
$data[$relation->getForeignKeyName()] = Arr::get($data, $relation->getRelationName()); |
118
|
|
|
unset($data[$relation->getRelationName()]); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $data; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Based on relation type returns the default field type. |
127
|
|
|
* |
128
|
|
|
* @param string $relation_type |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
|
|
public function inferFieldTypeFromFieldRelation($field) |
132
|
|
|
{ |
133
|
|
|
switch ($field['relation_type']) { |
134
|
|
|
case 'BelongsToMany': |
135
|
|
|
case 'HasMany': |
136
|
|
|
case 'HasManyThrough': |
137
|
|
|
case 'MorphMany': |
138
|
|
|
case 'MorphToMany': |
139
|
|
|
case 'BelongsTo': |
140
|
|
|
return 'relationship'; |
141
|
|
|
default: |
142
|
|
|
return 'text'; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Based on relation type returns if relation allows multiple entities. |
148
|
|
|
* |
149
|
|
|
* @param string $relation_type |
150
|
|
|
* @return bool |
151
|
|
|
*/ |
152
|
|
|
public function guessIfFieldHasMultipleFromRelationType($relation_type) |
153
|
|
|
{ |
154
|
|
|
switch ($relation_type) { |
155
|
|
|
case 'BelongsToMany': |
156
|
|
|
case 'HasMany': |
157
|
|
|
case 'HasManyThrough': |
158
|
|
|
case 'HasOneOrMany': |
159
|
|
|
case 'MorphMany': |
160
|
|
|
case 'MorphOneOrMany': |
161
|
|
|
case 'MorphToMany': |
162
|
|
|
return true; |
163
|
|
|
default: |
164
|
|
|
return false; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Based on relation type returns if relation has a pivot table. |
170
|
|
|
* |
171
|
|
|
* @param string $relation_type |
172
|
|
|
* @return bool |
173
|
|
|
*/ |
174
|
|
|
public function guessIfFieldHasPivotFromRelationType($relation_type) |
175
|
|
|
{ |
176
|
|
|
switch ($relation_type) { |
177
|
|
|
case 'BelongsToMany': |
178
|
|
|
case 'MorphToMany': |
179
|
|
|
return true; |
180
|
|
|
break; |
|
|
|
|
181
|
|
|
default: |
182
|
|
|
return false; |
183
|
|
|
break; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Check if field name contains a dot, if so, meaning it's a nested relation. |
189
|
|
|
* |
190
|
|
|
* @param array $field |
191
|
|
|
* @return bool |
192
|
|
|
*/ |
193
|
|
|
protected function isNestedRelation($field): bool |
194
|
|
|
{ |
195
|
|
|
return Str::contains($field['entity'], '.'); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Return the relation without any model attributes there. |
200
|
|
|
* Eg. user.entity_id would return user, as entity_id is not a relation in user. |
201
|
|
|
* |
202
|
|
|
* @param array $relation_field |
203
|
|
|
* @return string |
204
|
|
|
*/ |
205
|
|
|
public function getOnlyRelationEntity($relation_field) |
206
|
|
|
{ |
207
|
|
|
$relation_model = $this->getRelationModel($relation_field['entity'], -1); |
|
|
|
|
208
|
|
|
$related_method = Str::afterLast($relation_field['entity'], '.'); |
209
|
|
|
|
210
|
|
|
if (! method_exists($relation_model, $related_method) && $this->isNestedRelation($relation_field)) { |
211
|
|
|
return Str::beforeLast($relation_field['entity'], '.'); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
return $relation_field['entity']; |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|