1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\PanelTraits; |
4
|
|
|
|
5
|
|
|
trait Update |
6
|
|
|
{ |
7
|
|
|
/* |
8
|
|
|
|-------------------------------------------------------------------------- |
9
|
|
|
| UPDATE |
10
|
|
|
|-------------------------------------------------------------------------- |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Update a row in the database. |
15
|
|
|
* |
16
|
|
|
* @param [Int] The entity's id |
17
|
|
|
* @param [Request] All inputs to be updated. |
18
|
|
|
* |
19
|
|
|
* @return [Eloquent Collection] |
|
|
|
|
20
|
|
|
*/ |
21
|
2 |
|
public function update($id, $data) |
|
|
|
|
22
|
|
|
{ |
23
|
2 |
|
$data = $this->decodeJsonCastedAttributes($data, 'update', $id); |
|
|
|
|
24
|
1 |
|
$data = $this->compactFakeFields($data, 'update', $id); |
|
|
|
|
25
|
|
|
|
26
|
1 |
|
$item = $this->model->findOrFail($id); |
|
|
|
|
27
|
|
|
|
28
|
1 |
|
$this->syncPivot($item, $data, 'update'); |
|
|
|
|
29
|
|
|
|
30
|
|
|
// ommit the n-n relationships when updating the eloquent item |
31
|
1 |
|
$nn_relationships = array_pluck($this->getRelationFieldsWithPivot('update'), 'name'); |
|
|
|
|
32
|
1 |
|
$data = array_except($data, $nn_relationships); |
33
|
1 |
|
$updated = $item->update($data); |
|
|
|
|
34
|
|
|
|
35
|
1 |
|
return $item; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Get all fields needed for the EDIT ENTRY form. |
40
|
|
|
* |
41
|
|
|
* @param [integer] The id of the entry that is being edited. |
42
|
|
|
* @param int $id |
43
|
|
|
* |
44
|
|
|
* @return [array] The fields with attributes, fake attributes and values. |
|
|
|
|
45
|
|
|
*/ |
46
|
14 |
|
public function getUpdateFields($id) |
|
|
|
|
47
|
|
|
{ |
48
|
14 |
|
$fields = $this->update_fields; |
|
|
|
|
49
|
14 |
|
$entry = $this->getEntry($id); |
|
|
|
|
50
|
|
|
|
51
|
8 |
|
foreach ($fields as $k => $field) { |
52
|
|
|
// set the value |
53
|
7 |
|
if (! isset($fields[$k]['value'])) { |
54
|
7 |
|
if (isset($field['subfields'])) { |
55
|
|
|
$fields[$k]['value'] = []; |
56
|
|
|
foreach ($field['subfields'] as $key => $subfield) { |
57
|
|
|
$fields[$k]['value'][] = $entry->{$subfield['name']}; |
58
|
|
|
} |
59
|
|
|
} else { |
60
|
7 |
|
$fields[$k]['value'] = $entry->{$field['name']}; |
61
|
|
|
} |
62
|
7 |
|
} |
63
|
8 |
|
} |
64
|
|
|
|
65
|
|
|
// always have a hidden input for the entry id |
66
|
8 |
|
if (! array_key_exists('id', $fields)) { |
67
|
6 |
|
$fields['id'] = [ |
68
|
6 |
|
'name' => $entry->getKeyName(), |
69
|
6 |
|
'value' => $entry->getKey(), |
70
|
6 |
|
'type' => 'hidden', |
71
|
|
|
]; |
72
|
6 |
|
} |
73
|
|
|
|
74
|
8 |
|
return $fields; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
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.