1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Library\CrudPanel\Traits; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
6
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany; |
7
|
|
|
use Illuminate\Database\Eloquent\Relations\HasOne; |
8
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany; |
9
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphOne; |
10
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphToMany; |
11
|
|
|
use Illuminate\Support\Arr; |
12
|
|
|
|
13
|
|
|
trait Update |
14
|
|
|
{ |
15
|
|
|
/* |
16
|
|
|
|-------------------------------------------------------------------------- |
17
|
|
|
| UPDATE |
18
|
|
|
|-------------------------------------------------------------------------- |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Update a row in the database. |
23
|
|
|
* |
24
|
|
|
* @param int $id The entity's id |
25
|
|
|
* @param array $data All inputs to be updated. |
26
|
|
|
* |
27
|
|
|
* @return object |
28
|
|
|
*/ |
29
|
|
|
public function update($id, $data) |
30
|
|
|
{ |
31
|
|
|
$data = $this->decodeJsonCastedAttributes($data); |
|
|
|
|
32
|
|
|
$data = $this->compactFakeFields($data); |
|
|
|
|
33
|
|
|
$item = $this->model->findOrFail($id); |
34
|
|
|
|
35
|
|
|
$data = $this->changeBelongsToNamesFromRelationshipToForeignKey($data); |
|
|
|
|
36
|
|
|
|
37
|
|
|
$this->createRelations($item, $data); |
|
|
|
|
38
|
|
|
|
39
|
|
|
// omit the n-n relationships when updating the eloquent item |
40
|
|
|
$nn_relationships = Arr::pluck($this->getRelationFieldsWithPivot(), 'name'); |
|
|
|
|
41
|
|
|
|
42
|
|
|
$data = Arr::except($data, $nn_relationships); |
43
|
|
|
|
44
|
|
|
$updated = $item->update($data); |
|
|
|
|
45
|
|
|
|
46
|
|
|
return $item; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get all fields needed for the EDIT ENTRY form. |
51
|
|
|
* |
52
|
|
|
* @param int $id The id of the entry that is being edited. |
53
|
|
|
* |
54
|
|
|
* @return array The fields with attributes, fake attributes and values. |
55
|
|
|
*/ |
56
|
|
|
public function getUpdateFields($id = false) |
57
|
|
|
{ |
58
|
|
|
$fields = $this->fields(); |
|
|
|
|
59
|
|
|
$entry = ($id != false) ? $this->getEntry($id) : $this->getCurrentEntry(); |
|
|
|
|
60
|
|
|
|
61
|
|
|
foreach ($fields as &$field) { |
62
|
|
|
// set the value |
63
|
|
|
if (! isset($field['value'])) { |
64
|
|
|
if (isset($field['subfields'])) { |
65
|
|
|
$field['value'] = []; |
66
|
|
|
foreach ($field['subfields'] as $subfield) { |
67
|
|
|
$field['value'][] = $entry->{$subfield['name']}; |
68
|
|
|
} |
69
|
|
|
} else { |
70
|
|
|
$field['value'] = $this->getModelAttributeValue($entry, $field); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// always have a hidden input for the entry id |
76
|
|
|
if (! array_key_exists('id', $fields)) { |
77
|
|
|
$fields['id'] = [ |
78
|
|
|
'name' => $entry->getKeyName(), |
79
|
|
|
'value' => $entry->getKey(), |
80
|
|
|
'type' => 'hidden', |
81
|
|
|
]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $fields; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get the value of the 'name' attribute from the declared relation model in the given field. |
89
|
|
|
* |
90
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model The current CRUD model. |
91
|
|
|
* @param array $field The CRUD field array. |
92
|
|
|
* |
93
|
|
|
* @return mixed The value of the 'name' attribute from the relation model. |
94
|
|
|
*/ |
95
|
|
|
private function getModelAttributeValue($model, $field) |
96
|
|
|
{ |
97
|
|
|
if (isset($field['entity']) && $field['entity'] !== false) { |
98
|
|
|
$relational_entity = $this->parseRelationFieldNamesFromHtml([$field])[0]['name']; |
|
|
|
|
99
|
|
|
|
100
|
|
|
$relation_array = explode('.', $relational_entity); |
101
|
|
|
|
102
|
|
|
$relatedModel = array_reduce(array_splice($relation_array, 0, -1), function ($obj, $method) { |
103
|
|
|
return $obj->{$method} ? $obj->{$method} : $obj; |
104
|
|
|
}, $model); |
105
|
|
|
|
106
|
|
|
$relationMethod = Arr::last($relation_array); |
107
|
|
|
if (method_exists($relatedModel, $relationMethod)) { |
108
|
|
|
$relation = $relatedModel->{$relationMethod}(); |
109
|
|
|
$relation_type = get_class($relation); |
110
|
|
|
|
111
|
|
|
switch ($relation_type) { |
112
|
|
|
case HasOne::class: |
113
|
|
|
case MorphOne::class: |
114
|
|
|
return $relatedModel->{$relationMethod}->{Str::afterLast($relational_entity, '.')}; |
|
|
|
|
115
|
|
|
break; |
|
|
|
|
116
|
|
|
|
117
|
|
|
case HasMany::class: |
118
|
|
|
case MorphMany::class: |
119
|
|
|
case BelongsToMany::class: |
120
|
|
|
case MorphToMany::class: |
121
|
|
|
$attribute_value = $this->getManyRelationAttributeValue($relatedModel, $relationMethod, $field, $relation_type); |
122
|
|
|
// we only want to return the json_encoded values here |
123
|
|
|
if (is_string($attribute_value)) { |
124
|
|
|
return $attribute_value; |
125
|
|
|
} |
126
|
|
|
break; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $relatedModel->{$relationMethod}; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
if (is_string($field['name'])) { |
134
|
|
|
return $model->{$field['name']}; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
if (is_array($field['name'])) { |
138
|
|
|
$result = []; |
139
|
|
|
foreach ($field['name'] as $key => $value) { |
140
|
|
|
$result = $model->{$value}; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $result; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Returns the json encoded pivot fields from supported relations. |
149
|
|
|
* |
150
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
151
|
|
|
* @param string $relation_method |
152
|
|
|
* @param array $field |
153
|
|
|
* @param string $relation_type |
154
|
|
|
* @return bool|string |
155
|
|
|
*/ |
156
|
|
|
private function getManyRelationAttributeValue($model, $relation_method, $field, $relation_type) |
157
|
|
|
{ |
158
|
|
|
if (! isset($field['pivotFields']) || ! is_array($field['pivotFields'])) { |
159
|
|
|
return false; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
$pivot_fields = Arr::where($field['pivotFields'], function ($item) use ($field) { |
163
|
|
|
return $field['name'] != $item['name']; |
164
|
|
|
}); |
165
|
|
|
$related_models = $model->{$relation_method}; |
166
|
|
|
$result = []; |
167
|
|
|
|
168
|
|
|
// for any given model, we grab the attributes that belong to our pivot table. |
169
|
|
|
foreach ($related_models as $related_model) { |
170
|
|
|
$item = []; |
171
|
|
|
switch ($relation_type) { |
172
|
|
|
case HasMany::class: |
173
|
|
|
case MorphMany::class: |
174
|
|
|
// for any given related model, we get the value from pivot fields |
175
|
|
|
foreach ($pivot_fields as $pivot_field) { |
176
|
|
|
$item[$pivot_field['name']] = $related_model->{$pivot_field['name']}; |
177
|
|
|
} |
178
|
|
|
$item[$related_model->getKeyName()] = $related_model->getKey(); |
179
|
|
|
$result[] = $item; |
180
|
|
|
break; |
181
|
|
|
|
182
|
|
|
case BelongsToMany::class: |
183
|
|
|
case MorphToMany::class: |
184
|
|
|
// for any given related model, we get the pivot fields. |
185
|
|
|
foreach ($pivot_fields as $pivot_field) { |
186
|
|
|
$item[$pivot_field['name']] = $related_model->pivot->{$pivot_field['name']}; |
187
|
|
|
} |
188
|
|
|
$item[$field['name']] = $related_model->getKey(); |
189
|
|
|
$result[] = $item; |
190
|
|
|
break; |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
// we return the json encoded result as expected by repeatable field. |
195
|
|
|
return json_encode($result); |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|