QualityLabelController.php 3.7 KB

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