| Code Coverage
| Classes and Traits | Functions and Methods | Lines
Total | 0.00% covered (danger) | 0.00% | 0 / 1 | 0.00% covered (danger) | 0.00% | 0 / 9 | CRAP | 0.00% covered (danger) | 0.00% | 0 / 35
OwnerController | 0.00% covered (danger) | 0.00% | 0 / 1 | 0.00% covered (danger) | 0.00% | 0 / 9 | 272.00 | 0.00% covered (danger) | 0.00% | 0 / 35
index | 0.00% covered (danger) | 0.00% | 0 / 1 | 6.00 | 0.00% covered (danger) | 0.00% | 0 / 3
create | 0.00% covered (danger) | 0.00% | 0 / 1 | 6.00 | 0.00% covered (danger) | 0.00% | 0 / 2
store | 0.00% covered (danger) | 0.00% | 0 / 1 | 6.00 | 0.00% covered (danger) | 0.00% | 0 / 11
validatorCreate | 0.00% covered (danger) | 0.00% | 0 / 1 | 2.00 | 0.00% covered (danger) | 0.00% | 0 / 2
validatorUpdate | 0.00% covered (danger) | 0.00% | 0 / 1 | 2.00 | 0.00% covered (danger) | 0.00% | 0 / 2
show | 0.00% covered (danger) | 0.00% | 0 / 1 | 2.00 | 0.00% covered (danger) | 0.00% | 0 / 1
edit | 0.00% covered (danger) | 0.00% | 0 / 1 | 6.00 | 0.00% covered (danger) | 0.00% | 0 / 2
update | 0.00% covered (danger) | 0.00% | 0 / 1 | 6.00 | 0.00% covered (danger) | 0.00% | 0 / 6
destroy | 0.00% covered (danger) | 0.00% | 0 / 1 | 12.00 | 0.00% covered (danger) | 0.00% | 0 / 6

1 | <?php
2 |
3 | namespace App\Http\Controllers;
4 |
5 | use App\Authority;
6 | use App\Log;
7 | use App\Logistic;
8 | use App\Owner;
9 | use Exception;
10 | use Illuminate\Http\Request;
11 | use Illuminate\Http\Response;
12 | use Illuminate\Support\Facades\Auth;
13 | use Illuminate\Support\Facades\Gate;
14 | use Illuminate\Support\Facades\Validator;
15 |
16 | class OwnerController extends Controller
17 | {
18 | /**
19 | * Display a listing of the resource.
20 | *
21 | * @return Response
22 | */
23 | public function index()
24 | {
25 | if(!Gate::allows('货主-查询')){ return redirect(url('/')); }
26 | $owners=Owner::orderBy('id','desc')->paginate(25);
27 | return view('maintenance.owner.index',['owners'=>$owners]);
28 | }
29 |
30 |
31 | public function create()
32 | {
33 | if(!Gate::allows('货主-录入')){ return redirect(url('/')); }
34 | return view('maintenance.owner.create');
35 | }
36 |
37 | /**
38 | * Store a newly created resource in storage.
39 | *
40 | * @param Request $request
41 | * @return string
42 | */
43 | public function store(Request $request)
44 | {
45 | if(!Gate::allows('货主-录入')){ return redirect(url('/')); }
46 | $this->validatorCreate($request->all())->validate();
47 | $owner=new Owner($request->all());
48 | $owner->save();
49 | $authority=new Authority([
50 | 'name'=>"_{$owner['id']}",
51 | 'alias_name'=>"(货主：{$owner['name']})",
52 | 'remark'=>"(key: _{$owner['id']})",
53 | ]);
54 | $authority->save();
55 |
56 | $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
57 | return redirect('maintenance/owner/create')->with('successTip',$request->input('name')??'');
58 | }
59 | protected function validatorCreate(array $data)
60 | {
61 | return Validator::make($data, [
62 | 'code' => ['required', 'string', 'max:50', 'unique:owners'],
63 | 'name' => ['required', 'string', 'max:50'],
64 | ]);
65 | }
66 | protected function validatorUpdate(array $data)
67 | {
68 | return Validator::make($data, [
69 | 'name' => ['required', 'string', 'max:50'],
70 | ]);
71 | }
72 | /**
73 | * Display the specified resource.
74 | *
75 | * @param Owner $owner
76 | * @return Response
77 | */
78 | public function show(Owner $owner)
79 | {
80 | //
81 | }
82 |
83 | /**
84 | * Show the form for editing the specified resource.
85 | *
86 | * @param Owner $owner
87 | * @return Response
88 | */
89 | public function edit(Owner $owner)
90 | {
91 | if(!Gate::allows('货主-编辑')){ return redirect(url('/')); }
92 | return view('maintenance.owner.edit',['owner'=>$owner]);
93 | }
94 |
95 | /**
96 | * Update the specified resource in storage.
97 | *
98 | * @param Request $request
99 | * @param Owner $owner
100 | * @return Response
101 | */
102 | public function update(Request $request, Owner $owner)
103 | {
104 | if(!Gate::allows('货主-编辑')){ return redirect(url('/')); }
105 | $this->validatorUpdate($request->all())->validate();
106 | $owner->fill($request->all());
107 | $owner->update();
108 | $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
109 | return redirect('maintenance/owner/')->with('successTip',"成功修改货主{$owner['name']}！");
110 | }
111 |
112 | /**
113 | * Remove the specified resource from storage.
114 | *
115 | * @param Owner $owner
116 | * @return array|Response
117 | * @throws Exception
118 | */
119 | public function destroy(Owner $owner)
120 | {
121 | if(!Gate::allows('货主-删除')){ return redirect(url('/')); }
122 | $authority=Authority::where('name',"_{$owner['id']}")->first();
123 | if($authority)$authority->delete();
124 | $this->log(__METHOD__,__FUNCTION__,$owner->toJson(),Auth::user()['id']);
125 | $re=$owner->delete();
126 | return ['success'=>$re];
127 | }
128 | }