|
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
|
|
|
public function create($data) |
|
|
|
|
|
|
21
|
|
|
{ |
|
22
|
|
|
$data = $this->decodeJsonCastedAttributes($data, 'create'); |
|
|
|
|
|
|
23
|
|
|
$data = $this->compactFakeFields($data, 'create'); |
|
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
// ommit the n-n relationships when updating the eloquent item |
|
26
|
|
|
$nn_relationships = array_pluck($this->getRelationFieldsWithPivot('update'), 'name'); |
|
27
|
|
|
$item = $this->model->create(array_except($data, $nn_relationships)); |
|
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
// if there are any relationships available, also sync those |
|
30
|
|
|
$this->syncPivot($item, $data); |
|
31
|
|
|
|
|
32
|
|
|
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
|
|
|
public function getCreateFields() |
|
|
|
|
|
|
41
|
|
|
{ |
|
42
|
|
|
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
|
|
|
public function getRelationFields($form = 'create') |
|
53
|
|
|
{ |
|
54
|
|
|
if ($form == 'create') { |
|
55
|
|
|
$fields = $this->create_fields; |
|
56
|
|
|
} else { |
|
57
|
|
|
$fields = $this->update_fields; |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$relationFields = []; |
|
61
|
|
|
|
|
62
|
|
|
foreach ($fields as $field) { |
|
63
|
|
|
if (isset($field['model'])) { |
|
64
|
|
|
array_push($relationFields, $field); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
if (isset($field['subfields']) && |
|
68
|
|
|
is_array($field['subfields']) && |
|
69
|
|
|
count($field['subfields'])) { |
|
70
|
|
|
foreach ($field['subfields'] as $subfield) { |
|
71
|
|
|
array_push($relationFields, $subfield); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
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
|
|
|
public function getRelationFieldsWithPivot($form = 'create') |
|
87
|
|
|
{ |
|
88
|
|
|
$all_relation_fields = $this->getRelationFields($form); |
|
89
|
|
|
|
|
90
|
|
|
return array_where($all_relation_fields, function ($value, $key) { |
|
|
|
|
|
|
91
|
|
|
return isset($value['pivot']) && $value['pivot']; |
|
92
|
|
|
}); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function syncPivot($model, $data, $form = 'create') |
|
96
|
|
|
{ |
|
97
|
|
|
$fields_with_relationships = $this->getRelationFields($form); |
|
98
|
|
|
|
|
99
|
|
|
foreach ($fields_with_relationships as $key => $field) { |
|
100
|
|
|
if (isset($field['pivot']) && $field['pivot']) { |
|
101
|
|
|
$values = isset($data[$field['name']]) ? $data[$field['name']] : []; |
|
102
|
|
|
$model->{$field['name']}()->sync($values); |
|
103
|
|
|
|
|
104
|
|
|
if (isset($field['pivotFields'])) { |
|
105
|
|
|
foreach ($field['pivotFields'] as $pivotField) { |
|
106
|
|
|
foreach ($data[$pivotField] as $pivot_id => $field) { |
|
107
|
|
|
$model->{$field['name']}()->updateExistingPivot($pivot_id, [$pivotField => $field]); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
if (isset($field['morph']) && $field['morph']) { |
|
114
|
|
|
$values = isset($data[$field['name']]) ? $data[$field['name']] : []; |
|
115
|
|
|
if ($model->{$field['name']}) { |
|
116
|
|
|
$model->{$field['name']}()->update($values); |
|
117
|
|
|
} else { |
|
118
|
|
|
$model->{$field['name']}()->create($values); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
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.