TutorialController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Owner;
  4. use App\Tutorial;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\Gate;
  7. use Illuminate\Support\Facades\Validator;
  8. use Te7aHoudini\LaravelTrix\Models\TrixAttachment;
  9. use Te7aHoudini\LaravelTrix\Models\TrixRichText;
  10. class TutorialController extends Controller
  11. {
  12. /**
  13. * Display a listing of the resource.
  14. *
  15. * @return \Illuminate\Http\Response
  16. */
  17. public function index()
  18. {
  19. $tutorials=Tutorial::select('id','owner_id','name','type','created_at')->paginate(50);
  20. return view('maintenance.tutorial.index',['tutorials'=>$tutorials]);
  21. }
  22. /**
  23. * Show the form for creating a new resource.
  24. *
  25. * @param Request $request
  26. * @return \Illuminate\Http\Response
  27. */
  28. public function create(Request $request)
  29. {
  30. $response=[];
  31. if ($request->input('owner_id'))$response['owner_id']=$request->input('owner_id');
  32. $owners=Owner::get();
  33. $response['owners']=$owners;
  34. return view('maintenance.tutorial.create',$response);
  35. }
  36. /**
  37. * Store a newly created resource in storage.
  38. *
  39. * @param \Illuminate\Http\Request $request
  40. * @return \Illuminate\Http\Response
  41. */
  42. public function store(Request $request)
  43. {
  44. if(!Gate::allows('教程-录入')){ return redirect(url('/')); }
  45. $this->validator($request)->validate();
  46. $tutorial=Tutorial::create(['owner_id'=>$request->input('owner_id'),'name'=>$request->input('name')]);
  47. $tutorial['tutorial-trixFields']=request('tutorial-trixFields');
  48. $tutorial['attachment-tutorial-trixFields']=request('attachment-tutorial-trixFields');
  49. $tutorial->save();
  50. return redirect('maintenance/tutorial')->with('successTip','新教程“'.$request->input('name').'”添加成功');
  51. }
  52. //展示教程内容
  53. public function showContent($id){
  54. if(!Gate::allows('教程-查询')){ return redirect(url('/')); }
  55. $tutorial=Tutorial::with('trixRichText')->find($id);
  56. if ($tutorial&&$tutorial->trixRichText)return ['success'=>true,'data'=>$tutorial->trixRichText->content];
  57. return ['success'=>false];
  58. }
  59. /**
  60. * Display the specified resource.
  61. *
  62. * @param int $id
  63. * @return \Illuminate\Http\Response
  64. */
  65. public function show($id)
  66. {
  67. $tutorial=Tutorial::with('trixRichText')->find($id);
  68. return view('maintenance.tutorial.show',compact('tutorial'));
  69. }
  70. /**
  71. * Show the form for editing the specified resource.
  72. *
  73. * @param int $id
  74. * @return \Illuminate\Http\Response
  75. */
  76. public function edit($id)
  77. {
  78. if(!Gate::allows('教程-编辑')){ return redirect(url('/')); }
  79. $tutorial=Tutorial::with('trixRichText')->find($id);
  80. $owners=Owner::get();
  81. return view('maintenance.tutorial.edit',['owners'=>$owners,'tutorial'=>$tutorial]);
  82. }
  83. /**
  84. * Update the specified resource in storage.
  85. *
  86. * @param \Illuminate\Http\Request $request
  87. * @param int $id
  88. * @return \Illuminate\Http\Response
  89. */
  90. public function update(Request $request, $id)
  91. {
  92. if(!Gate::allows('教程-编辑')){ return redirect(url('/')); }
  93. $this->validator($request)->validate();
  94. $tutorial=Tutorial::find($id);
  95. $tutorial->owner_id=$request->input('owner_id');
  96. $tutorial->name=$request->input('name');
  97. $tutorial['tutorial-trixFields']=request('tutorial-trixFields');
  98. $tutorial['attachment-tutorial-trixFields']=request('attachment-tutorial-trixFields');
  99. $tutorial->update();
  100. return redirect('maintenance/tutorial')->with('successTip','教程“'.$request->input('name').'”修改成功');
  101. }
  102. /**
  103. * Remove the specified resource from storage.
  104. *
  105. * @param int $id
  106. * @return array
  107. */
  108. public function destroy($id)
  109. {
  110. if(!Gate::allows('教程-删除')){ return redirect(url('/')); }
  111. $tutorial=Tutorial::find($id);
  112. $tutorial->delete();
  113. TrixRichText::where('model_id',$id)->delete();
  114. $attachments = TrixAttachment::where('attachable_id', $id)->get();
  115. foreach ($attachments as $attachment){
  116. optional($attachment)->purge();
  117. }
  118. return ['success'=>true];
  119. }
  120. public function validator(Request $request){
  121. $validator=Validator::make($request->input(),[
  122. 'name'=>['required','max:50'],
  123. 'owner_id'=>['required','integer'],
  124. ],[
  125. 'required'=>':attribute 为必填项',
  126. 'max'=>':attribute 输入过长',
  127. ],[
  128. 'name'=>'标题',
  129. 'owner_id'=>'货主',
  130. ]);
  131. return $validator;
  132. }
  133. }