1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Http\Controllers\Operations; |
4
|
|
|
|
5
|
|
|
trait Create |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Show the form for creating inserting a new row. |
9
|
|
|
* |
10
|
|
|
* @return Response |
|
|
|
|
11
|
|
|
*/ |
12
|
|
|
public function create() |
13
|
|
|
{ |
14
|
|
|
$this->crud->hasAccessOrFail('create'); |
|
|
|
|
15
|
|
|
|
16
|
|
|
// prepare the fields you need to show |
17
|
|
|
$this->data['crud'] = $this->crud; |
|
|
|
|
18
|
|
|
$this->data['saveAction'] = $this->getSaveAction(); |
|
|
|
|
19
|
|
|
$this->data['fields'] = $this->crud->getCreateFields(); |
20
|
|
|
$this->data['title'] = trans('backpack::crud.add').' '.$this->crud->entity_name; |
21
|
|
|
|
22
|
|
|
// load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package |
23
|
|
|
return view($this->crud->getCreateView(), $this->data); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Store a newly created resource in the database. |
28
|
|
|
* |
29
|
|
|
* @param StoreRequest $request - type injection used for validation using Requests |
|
|
|
|
30
|
|
|
* |
31
|
|
|
* @return \Illuminate\Http\RedirectResponse |
32
|
|
|
*/ |
33
|
|
|
public function storeCrud(StoreRequest $request = null) |
34
|
|
|
{ |
35
|
|
|
$this->crud->hasAccessOrFail('create'); |
36
|
|
|
|
37
|
|
|
// fallback to global request instance |
38
|
|
|
if (is_null($request)) { |
39
|
|
|
$request = \Request::instance(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
// replace empty values with NULL, so that it will work with MySQL strict mode on |
43
|
|
View Code Duplication |
foreach ($request->input() as $key => $value) { |
|
|
|
|
44
|
|
|
if (empty($value) && $value !== '0') { |
45
|
|
|
$request->request->set($key, null); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
// insert item in the db |
50
|
|
|
$item = $this->crud->create($request->except(['save_action', '_token', '_method', 'current_tab'])); |
51
|
|
|
$this->data['entry'] = $this->crud->entry = $item; |
52
|
|
|
|
53
|
|
|
// show a success message |
54
|
|
|
\Alert::success(trans('backpack::crud.insert_success'))->flash(); |
55
|
|
|
|
56
|
|
|
// save the redirect choice for next time |
57
|
|
|
$this->setSaveAction(); |
|
|
|
|
58
|
|
|
|
59
|
|
|
return $this->performSaveAction($item->getKey()); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.