1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\PanelTraits; |
4
|
|
|
|
5
|
|
|
trait Create |
6
|
|
|
{ |
7
|
|
|
/* |
8
|
|
|
|-------------------------------------------------------------------------- |
9
|
|
|
| CREATE |
10
|
|
|
|-------------------------------------------------------------------------- |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Insert a row in the database. |
15
|
|
|
* |
16
|
|
|
* @param [Request] All input values to be inserted. |
17
|
|
|
* |
18
|
|
|
* @return [Eloquent Collection] |
|
|
|
|
19
|
|
|
*/ |
20
|
4 |
|
public function create($data) |
|
|
|
|
21
|
|
|
{ |
22
|
4 |
|
$data = $this->decodeJsonCastedAttributes($data, 'create'); |
|
|
|
|
23
|
4 |
|
$data = $this->compactFakeFields($data, 'create'); |
|
|
|
|
24
|
|
|
|
25
|
|
|
// ommit the n-n relationships when updating the eloquent item |
26
|
4 |
|
$nn_relationships = array_pluck($this->getRelationFieldsWithPivot('create'), 'name'); |
27
|
4 |
|
$item = $this->model->create(array_except($data, $nn_relationships)); |
|
|
|
|
28
|
|
|
|
29
|
|
|
// if there are any relationships available, also sync those |
30
|
4 |
|
$this->syncPivot($item, $data); |
31
|
|
|
|
32
|
4 |
|
return $item; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Get all fields needed for the ADD NEW ENTRY form. |
37
|
|
|
* |
38
|
|
|
* @return [array] The fields with attributes and fake attributes. |
|
|
|
|
39
|
|
|
*/ |
40
|
22 |
|
public function getCreateFields() |
|
|
|
|
41
|
|
|
{ |
42
|
22 |
|
return $this->create_fields; |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Get all fields with relation set (model key set on field). |
47
|
|
|
* |
48
|
|
|
* @param [string: create/update/both] |
49
|
|
|
* |
50
|
|
|
* @return [array] The fields with model key set. |
|
|
|
|
51
|
|
|
*/ |
52
|
15 |
|
public function getRelationFields($form = 'create') |
53
|
|
|
{ |
54
|
15 |
|
if ($form == 'create') { |
55
|
13 |
|
$fields = $this->create_fields; |
56
|
|
|
} else { |
57
|
2 |
|
$fields = $this->update_fields; |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
15 |
|
$relationFields = []; |
61
|
|
|
|
62
|
15 |
|
foreach ($fields as $field) { |
63
|
14 |
|
if (isset($field['model'])) { |
64
|
8 |
|
array_push($relationFields, $field); |
65
|
|
|
} |
66
|
|
|
|
67
|
14 |
|
if (isset($field['subfields']) && |
68
|
14 |
|
is_array($field['subfields']) && |
69
|
14 |
|
count($field['subfields'])) { |
70
|
|
|
foreach ($field['subfields'] as $subfield) { |
71
|
14 |
|
array_push($relationFields, $subfield); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
15 |
|
return $relationFields; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get all fields with n-n relation set (pivot table is true). |
81
|
|
|
* |
82
|
|
|
* @param [string: create/update/both] |
83
|
|
|
* |
84
|
|
|
* @return [array] The fields with n-n relationships. |
|
|
|
|
85
|
|
|
*/ |
86
|
7 |
|
public function getRelationFieldsWithPivot($form = 'create') |
87
|
|
|
{ |
88
|
7 |
|
$all_relation_fields = $this->getRelationFields($form); |
89
|
|
|
|
90
|
7 |
|
return array_where($all_relation_fields, function ($value, $key) { |
|
|
|
|
91
|
3 |
|
return isset($value['pivot']) && $value['pivot']; |
92
|
7 |
|
}); |
93
|
|
|
} |
94
|
|
|
|
95
|
8 |
|
public function syncPivot($model, $data, $form = 'create') |
96
|
|
|
{ |
97
|
8 |
|
$fields_with_relationships = $this->getRelationFields($form); |
98
|
|
|
|
99
|
8 |
|
foreach ($fields_with_relationships as $key => $field) { |
100
|
4 |
|
if (isset($field['pivot']) && $field['pivot']) { |
101
|
3 |
|
$values = isset($data[$field['name']]) ? $data[$field['name']] : []; |
102
|
3 |
|
$model->{$field['name']}()->sync($values); |
103
|
|
|
|
104
|
2 |
|
if (isset($field['pivotFields'])) { |
105
|
|
|
foreach ($field['pivotFields'] as $pivotField) { |
106
|
|
|
foreach ($data[$pivotField] as $pivot_id => $pivot_field) { |
107
|
|
|
$model->{$field['name']}()->updateExistingPivot($pivot_id, [$pivotField => $pivot_field]); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
3 |
|
if (isset($field['morph']) && $field['morph'] && isset($data[$field['name']])) { |
114
|
|
|
$values = $data[$field['name']]; |
115
|
3 |
|
$model->{$field['name']}()->sync($values); |
116
|
|
|
} |
117
|
|
|
} |
118
|
7 |
|
} |
119
|
|
|
} |
120
|
|
|
|
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.