1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\PanelTraits; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Relations\HasOne; |
6
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany; |
7
|
|
|
|
8
|
|
|
trait Update |
9
|
|
|
{ |
10
|
|
|
/* |
11
|
|
|
|-------------------------------------------------------------------------- |
12
|
|
|
| UPDATE |
13
|
|
|
|-------------------------------------------------------------------------- |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Update a row in the database. |
18
|
|
|
* |
19
|
|
|
* @param [Int] The entity's id |
20
|
|
|
* @param [Request] All inputs to be updated. |
21
|
|
|
* |
22
|
|
|
* @return [Eloquent Collection] |
|
|
|
|
23
|
|
|
*/ |
24
|
2 |
|
public function update($id, $data) |
|
|
|
|
25
|
|
|
{ |
26
|
2 |
|
$data = $this->decodeJsonCastedAttributes($data, 'update', $id); |
|
|
|
|
27
|
1 |
|
$data = $this->compactFakeFields($data, 'update', $id); |
|
|
|
|
28
|
|
|
|
29
|
1 |
|
$item = $this->model->findOrFail($id); |
|
|
|
|
30
|
|
|
|
31
|
1 |
|
$this->createRelations($item, $data, 'update'); |
|
|
|
|
32
|
|
|
|
33
|
|
|
// omit the n-n relationships when updating the eloquent item |
34
|
1 |
|
$nn_relationships = array_pluck($this->getRelationFieldsWithPivot('update'), 'name'); |
|
|
|
|
35
|
1 |
|
$data = array_except($data, $nn_relationships); |
36
|
1 |
|
$updated = $item->update($data); |
|
|
|
|
37
|
|
|
|
38
|
1 |
|
return $item; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Get all fields needed for the EDIT ENTRY form. |
43
|
|
|
* |
44
|
|
|
* @param int $id The id of the entry that is being edited. |
45
|
|
|
* |
46
|
|
|
* @return array The fields with attributes, fake attributes and values. |
47
|
|
|
*/ |
48
|
14 |
|
public function getUpdateFields($id) |
49
|
|
|
{ |
50
|
14 |
|
$fields = $this->update_fields; |
|
|
|
|
51
|
14 |
|
$entry = $this->getEntry($id); |
|
|
|
|
52
|
|
|
|
53
|
8 |
|
foreach ($fields as &$field) { |
54
|
|
|
// set the value |
55
|
7 |
|
if (! isset($field['value'])) { |
56
|
7 |
|
if (isset($field['subfields'])) { |
57
|
|
|
$field['value'] = []; |
58
|
|
|
foreach ($field['subfields'] as $subfield) { |
59
|
|
|
$field['value'][] = $entry->{$subfield['name']}; |
60
|
|
|
} |
61
|
|
|
} else { |
62
|
7 |
|
$field['value'] = $this->getModelAttributeValue($entry, $field); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// always have a hidden input for the entry id |
68
|
8 |
|
if (! array_key_exists('id', $fields)) { |
69
|
6 |
|
$fields['id'] = [ |
70
|
6 |
|
'name' => $entry->getKeyName(), |
71
|
6 |
|
'value' => $entry->getKey(), |
72
|
6 |
|
'type' => 'hidden', |
73
|
|
|
]; |
74
|
|
|
} |
75
|
|
|
|
76
|
8 |
|
return $fields; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get the value of the 'name' attribute from the declared relation model in the given field. |
81
|
|
|
* |
82
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model The current CRUD model. |
83
|
|
|
* @param array $field The CRUD field array. |
84
|
|
|
* |
85
|
|
|
* @return mixed The value of the 'name' attribute from the relation model. |
86
|
|
|
*/ |
87
|
7 |
|
private function getModelAttributeValue($model, $field) |
88
|
|
|
{ |
89
|
7 |
|
if (isset($field['entity'])) { |
90
|
|
|
$relationArray = explode('.', $field['entity']); |
91
|
|
|
$relatedModel = array_reduce(array_splice($relationArray, 0, -1), function ($obj, $method) { |
92
|
|
|
return $obj->{$method} ? $obj->{$method} : $obj; |
93
|
|
|
}, $model); |
94
|
|
|
|
95
|
|
|
$relationMethod = end($relationArray); |
96
|
|
|
if ($relatedModel->{$relationMethod} && $relatedModel->{$relationMethod}() instanceof HasOne) { |
97
|
|
|
return $relatedModel->{$relationMethod}->getKey(); |
98
|
|
|
} elseif ($relatedModel->{$relationMethod} && $relatedModel->{$relationMethod}() instanceof HasMany) { |
99
|
|
|
return $relatedModel->{$relationMethod}; |
100
|
|
|
} else { |
101
|
|
|
return $relatedModel->{$field['name']}; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
7 |
|
return $model->{$field['name']}; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.