1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Http\Controllers\Operations; |
4
|
|
|
|
5
|
|
|
trait Update |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Show the form for editing the specified resource. |
9
|
|
|
* |
10
|
|
|
* @param int $id |
11
|
|
|
* |
12
|
|
|
* @return Response |
|
|
|
|
13
|
|
|
*/ |
14
|
|
|
public function edit($id) |
15
|
|
|
{ |
16
|
|
|
$this->crud->hasAccessOrFail('update'); |
|
|
|
|
17
|
|
|
|
18
|
|
|
// get entry ID from Request (makes sure its the last ID for nested resources) |
19
|
|
|
$id = $this->crud->getCurrentEntryId() ?? $id; |
20
|
|
|
|
21
|
|
|
// get the info for that entry |
22
|
|
|
$this->data['entry'] = $this->crud->getEntry($id); |
|
|
|
|
23
|
|
|
$this->data['crud'] = $this->crud; |
24
|
|
|
$this->data['saveAction'] = $this->getSaveAction(); |
|
|
|
|
25
|
|
|
$this->data['fields'] = $this->crud->getUpdateFields($id); |
26
|
|
|
$this->data['title'] = trans('backpack::crud.edit').' '.$this->crud->entity_name; |
27
|
|
|
|
28
|
|
|
$this->data['id'] = $id; |
29
|
|
|
|
30
|
|
|
// load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package |
31
|
|
|
return view($this->crud->getEditView(), $this->data); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Update the specified resource in the database. |
36
|
|
|
* |
37
|
|
|
* @param UpdateRequest $request - type injection used for validation using Requests |
|
|
|
|
38
|
|
|
* |
39
|
|
|
* @return \Illuminate\Http\RedirectResponse |
40
|
|
|
*/ |
41
|
|
|
public function updateCrud(UpdateRequest $request = null) |
42
|
|
|
{ |
43
|
|
|
$this->crud->hasAccessOrFail('update'); |
44
|
|
|
|
45
|
|
|
// fallback to global request instance |
46
|
|
|
if (is_null($request)) { |
47
|
|
|
$request = \Request::instance(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
// replace empty values with NULL, so that it will work with MySQL strict mode on |
51
|
|
View Code Duplication |
foreach ($request->input() as $key => $value) { |
|
|
|
|
52
|
|
|
if (empty($value) && $value !== '0') { |
53
|
|
|
$request->request->set($key, null); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// update the row in the db |
58
|
|
|
$item = $this->crud->update($request->get($this->crud->model->getKeyName()), |
59
|
|
|
$request->except('save_action', '_token', '_method', 'current_tab')); |
60
|
|
|
$this->data['entry'] = $this->crud->entry = $item; |
61
|
|
|
|
62
|
|
|
// show a success message |
63
|
|
|
\Alert::success(trans('backpack::crud.update_success'))->flash(); |
64
|
|
|
|
65
|
|
|
// save the redirect choice for next time |
66
|
|
|
$this->setSaveAction(); |
|
|
|
|
67
|
|
|
|
68
|
|
|
return $this->performSaveAction($item->getKey()); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
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.