1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* The Crud Controller. |
4
|
|
|
*/ |
5
|
|
|
namespace Backpack\Crud\Http\Controllers; |
6
|
|
|
|
7
|
|
|
use Backpack\Crud\Crud; |
8
|
|
|
use Illuminate\Routing\Controller as BaseController; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* The controller that handles all the crud actions. |
12
|
|
|
*/ |
13
|
|
View Code Duplication |
class ToneCrudNestedController extends BaseController |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
protected $crud; |
16
|
|
|
protected $data; |
17
|
|
|
|
18
|
|
|
public function __construct() |
19
|
|
|
{ |
20
|
|
|
$this->crud = new Crud(); |
21
|
|
|
$this->data = []; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Display a listing of the resource. |
26
|
|
|
* |
27
|
|
|
* @return \Illuminate\Http\Response |
28
|
|
|
*/ |
29
|
|
|
public function index($parentId) |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
// dd($this->crud); |
|
|
|
|
32
|
|
|
$this->crud->checkPermission('list'); |
|
|
|
|
33
|
|
|
|
34
|
|
|
return view('crud::layouts.list', $this->data + ['crud' => $this->crud]); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Show the form for creating a new resource. |
39
|
|
|
* |
40
|
|
|
* @return \Illuminate\Http\Response |
41
|
|
|
*/ |
42
|
|
|
public function create($parentId) |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
// dd($this->crud); |
|
|
|
|
45
|
|
|
$this->crud->checkPermission('add'); |
|
|
|
|
46
|
|
|
|
47
|
|
|
return view('crud::layouts.create', $this->data + ['crud' => $this->crud]); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Store a newly created resource in storage. |
52
|
|
|
* |
53
|
|
|
* @param \Illuminate\Http\Request $request |
54
|
|
|
* |
55
|
|
|
* @return \Illuminate\Http\Response |
56
|
|
|
*/ |
57
|
|
|
public function crudStore(Request $request = null) |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
// $this->crud->checkPermission('add'); |
|
|
|
|
60
|
|
|
// dd($request); |
|
|
|
|
61
|
|
|
$entity = $this->crud->save(\Request::all()); |
|
|
|
|
62
|
|
|
|
63
|
|
|
// return \Redirect::to($this->crud->getRoute()."/{$entity->id}/edit")->with('status', trans('crud::crud.form.create_success')); |
|
|
|
|
64
|
|
|
return \Redirect::to($this->crud->getRoute())->with('status', trans('crud::crud.form.create_success')); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Display the specified resource. |
69
|
|
|
* |
70
|
|
|
* @param int $id |
71
|
|
|
* |
72
|
|
|
* @return \Illuminate\Http\Response |
73
|
|
|
*/ |
74
|
|
|
public function show($parentId, $id) |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
$this->crud->checkPermission('view'); |
|
|
|
|
77
|
|
|
$this->crud->item($id); |
|
|
|
|
78
|
|
|
|
79
|
|
|
return view('crud::layouts.show', $this->data + ['crud' => $this->crud]); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Show the form for editing the specified resource. |
84
|
|
|
* |
85
|
|
|
* @param int $id |
86
|
|
|
* |
87
|
|
|
* @return \Illuminate\Http\Response |
88
|
|
|
*/ |
89
|
|
|
public function edit($parentId, $id) |
|
|
|
|
90
|
|
|
{ |
91
|
|
|
$this->crud->checkPermission('edit'); |
|
|
|
|
92
|
|
|
$this->crud->item($id); |
|
|
|
|
93
|
|
|
|
94
|
|
|
// dd($this->crud->item->toArray()); |
|
|
|
|
95
|
|
|
|
96
|
|
|
return view('crud::layouts.edit', $this->data + ['crud' => $this->crud]); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Update the specified resource in storage. |
101
|
|
|
* |
102
|
|
|
* @param \Illuminate\Http\Request $request |
103
|
|
|
* @param int $id |
104
|
|
|
* |
105
|
|
|
* @return \Illuminate\Http\Response |
106
|
|
|
*/ |
107
|
|
|
public function crudUpdate($parentId, $id, Request $request = null) |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
$this->crud->checkPermission('edit'); |
|
|
|
|
110
|
|
|
$this->crud->item($id); |
|
|
|
|
111
|
|
|
|
112
|
|
|
$entity = $this->crud->update($id, \Request::all()); |
|
|
|
|
113
|
|
|
|
114
|
|
|
return \Redirect::to($this->crud->getRoute())->with('status', trans('crud::crud.form.save_success')); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Remove the specified resource from storage. |
119
|
|
|
* |
120
|
|
|
* @param int $id |
121
|
|
|
* |
122
|
|
|
* @return \Illuminate\Http\Response |
123
|
|
|
*/ |
124
|
|
|
public function destroy($parentId, $id) |
|
|
|
|
125
|
|
|
{ |
126
|
|
|
$this->crud->checkPermission('delete'); |
|
|
|
|
127
|
|
|
|
128
|
|
|
return $this->crud->delete($id); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.