1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Http\Controllers\Operations; |
4
|
|
|
|
5
|
|
|
use Backpack\CRUD\app\Library\CrudPanel\Hooks\Facades\LifecycleHook; |
6
|
|
|
use Illuminate\Support\Facades\Route; |
7
|
|
|
|
8
|
|
|
trait CreateOperation |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Define which routes are needed for this operation. |
12
|
|
|
* |
13
|
|
|
* @param string $segment Name of the current entity (singular). Used as first URL segment. |
14
|
|
|
* @param string $routeName Prefix of the route name. |
15
|
|
|
* @param string $controller Name of the current CrudController. |
16
|
|
|
*/ |
17
|
|
|
protected function setupCreateRoutes($segment, $routeName, $controller) |
18
|
|
|
{ |
19
|
|
|
Route::get($segment.'/create', [ |
20
|
|
|
'as' => $routeName.'.create', |
21
|
|
|
'uses' => $controller.'@create', |
22
|
|
|
'operation' => 'create', |
23
|
|
|
]); |
24
|
|
|
|
25
|
|
|
Route::post($segment, [ |
26
|
|
|
'as' => $routeName.'.store', |
27
|
|
|
'uses' => $controller.'@store', |
28
|
|
|
'operation' => 'create', |
29
|
|
|
]); |
30
|
|
|
|
31
|
|
|
Route::get($segment.'/create-form', [ |
32
|
|
|
'as' => $routeName.'.create-form', |
33
|
|
|
'uses' => $controller.'@createForm', |
34
|
|
|
'operation' => 'create', |
35
|
|
|
]); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Add the default settings, buttons, etc that this operation needs. |
40
|
|
|
*/ |
41
|
|
|
protected function setupCreateDefaults() |
42
|
|
|
{ |
43
|
|
|
$this->crud->allowAccess('create'); |
44
|
|
|
|
45
|
|
|
LifecycleHook::hookInto('create:before_setup', function () { |
|
|
|
|
46
|
|
|
$this->crud->setupDefaultSaveActions(); |
47
|
|
|
}); |
48
|
|
|
|
49
|
|
|
LifecycleHook::hookInto('list:before_setup', function () { |
50
|
|
|
$this->crud->addButton('top', 'create', 'view', 'crud::buttons.create'); |
51
|
|
|
}); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Show the form for creating inserting a new row. |
56
|
|
|
* |
57
|
|
|
* @return \Illuminate\Contracts\View\View |
58
|
|
|
*/ |
59
|
|
|
public function create() |
60
|
|
|
{ |
61
|
|
|
$this->crud->hasAccessOrFail('create'); |
62
|
|
|
|
63
|
|
|
// prepare the fields you need to show |
64
|
|
|
$this->data['crud'] = $this->crud; |
|
|
|
|
65
|
|
|
$this->data['saveAction'] = $this->crud->getSaveAction(); |
66
|
|
|
$this->data['title'] = $this->crud->getTitle() ?? trans('backpack::crud.add').' '.$this->crud->entity_name; |
67
|
|
|
|
68
|
|
|
// load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package |
69
|
|
|
return view($this->crud->getCreateView(), $this->data); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function createForm() |
73
|
|
|
{ |
74
|
|
|
$this->crud->hasAccessOrFail('create'); |
75
|
|
|
|
76
|
|
|
// if the request isn't an AJAX request, return a 404 |
77
|
|
|
if (! request()->ajax()) { |
78
|
|
|
abort(404); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return view( |
82
|
|
|
$this->crud->getFirstFieldView('form.create_form'), |
83
|
|
|
[ |
84
|
|
|
'fields' => $this->crud->getCreateFields(), |
85
|
|
|
'action' => 'create', |
86
|
|
|
'crud' => $this->crud, |
87
|
|
|
'modalClass' => request()->get('modal_class'), |
88
|
|
|
'parentLoadedAssets' => request()->get('parent_loaded_assets'), |
89
|
|
|
] |
90
|
|
|
); |
91
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Store a newly created resource in the database. |
98
|
|
|
* |
99
|
|
|
* @return \Illuminate\Http\RedirectResponse |
100
|
|
|
*/ |
101
|
|
|
public function store() |
102
|
|
|
{ |
103
|
|
|
$this->crud->hasAccessOrFail('create'); |
104
|
|
|
|
105
|
|
|
// execute the FormRequest authorization and validation, if one is required |
106
|
|
|
$request = $this->crud->validateRequest(); |
107
|
|
|
|
108
|
|
|
// register any Model Events defined on fields |
109
|
|
|
$this->crud->registerFieldEvents(); |
110
|
|
|
|
111
|
|
|
// insert item in the db |
112
|
|
|
$item = $this->crud->create($this->crud->getStrippedSaveRequest($request)); |
113
|
|
|
$this->data['entry'] = $this->crud->entry = $item; |
|
|
|
|
114
|
|
|
|
115
|
|
|
// show a success message |
116
|
|
|
\Alert::success(trans('backpack::crud.insert_success'))->flash(); |
|
|
|
|
117
|
|
|
|
118
|
|
|
// save the redirect choice for next time |
119
|
|
|
$this->crud->setSaveAction(); |
120
|
|
|
|
121
|
|
|
return $this->crud->performSaveAction($item->getKey()); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|