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
|
|
|
$values_to_store = $this->compactFakeFields($data, 'create'); |
|
|
|
|
23
|
|
|
$item = $this->model->create($values_to_store); |
|
|
|
|
24
|
|
|
|
25
|
|
|
// if there are any relationships available, also sync those |
26
|
|
|
$this->syncPivot($item, $data); |
27
|
|
|
|
28
|
|
|
return $item; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Get all fields needed for the ADD NEW ENTRY form. |
33
|
|
|
* |
34
|
|
|
* @return [array] The fields with attributes and fake attributes. |
|
|
|
|
35
|
|
|
*/ |
36
|
|
|
public function getCreateFields() |
|
|
|
|
37
|
|
|
{ |
38
|
|
|
return $this->create_fields; |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Get all fields with relation set (model key set on field). |
43
|
|
|
* |
44
|
|
|
* @param [string: create/update/both] |
45
|
|
|
* |
46
|
|
|
* @return [array] The fields with model key set. |
|
|
|
|
47
|
|
|
*/ |
48
|
|
|
public function getRelationFields($form = 'create') |
49
|
|
|
{ |
50
|
|
|
if ($form == 'create') { |
51
|
|
|
$fields = $this->create_fields; |
52
|
|
|
} else { |
53
|
|
|
$fields = $this->update_fields; |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$relationFields = []; |
57
|
|
|
|
58
|
|
|
foreach ($fields as $field) { |
59
|
|
|
if (isset($field['model'])) { |
60
|
|
|
array_push($relationFields, $field); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if (isset($field['subfields']) && |
64
|
|
|
is_array($field['subfields']) && |
65
|
|
|
count($field['subfields'])) { |
66
|
|
|
foreach ($field['subfields'] as $subfield) { |
67
|
|
|
array_push($relationFields, $subfield); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $relationFields; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function syncPivot($model, $data, $form = 'create') |
76
|
|
|
{ |
77
|
|
|
$fields_with_relationships = $this->getRelationFields($form); |
78
|
|
|
|
79
|
|
|
foreach ($fields_with_relationships as $key => $field) { |
80
|
|
|
if (isset($field['pivot']) && $field['pivot']) { |
81
|
|
|
$values = isset($data[$field['name']]) ? $data[$field['name']] : []; |
82
|
|
|
$model->{$field['name']}()->sync($values); |
83
|
|
|
|
84
|
|
|
if (isset($field['pivotFields'])) { |
85
|
|
|
foreach ($field['pivotFields'] as $pivotField) { |
86
|
|
|
foreach ($data[$pivotField] as $pivot_id => $field) { |
87
|
|
|
$model->{$field['name']}()->updateExistingPivot($pivot_id, [$pivotField => $field]); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if (isset($field['morph']) && $field['morph']) { |
94
|
|
|
$values = isset($data[$field['name']]) ? $data[$field['name']] : []; |
95
|
|
|
if($model->{$field['name']}) { |
96
|
|
|
$model->{$field['name']}()->update($values); |
97
|
|
|
} else { |
98
|
|
|
$model->{$field['name']}()->create($values); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
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.