TutorialController.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. }
  68. /**
  69. * Show the form for editing the specified resource.
  70. *
  71. * @param int $id
  72. * @return \Illuminate\Http\Response
  73. */
  74. public function edit($id)
  75. {
  76. if(!Gate::allows('教程-编辑')){ return redirect(url('/')); }
  77. $tutorial=Tutorial::with('trixRichText')->find($id);
  78. $owners=Owner::get();
  79. return view('maintenance.tutorial.edit',['owners'=>$owners,'tutorial'=>$tutorial]);
  80. }
  81. /**
  82. * Update the specified resource in storage.
  83. *
  84. * @param \Illuminate\Http\Request $request
  85. * @param int $id
  86. * @return \Illuminate\Http\Response
  87. */
  88. public function update(Request $request, $id)
  89. {
  90. if(!Gate::allows('教程-编辑')){ return redirect(url('/')); }
  91. $this->validator($request)->validate();
  92. $tutorial=Tutorial::find($id);
  93. $tutorial->owner_id=$request->input('owner_id');
  94. $tutorial->name=$request->input('name');
  95. $tutorial['tutorial-trixFields']=request('tutorial-trixFields');
  96. $tutorial['attachment-tutorial-trixFields']=request('attachment-tutorial-trixFields');
  97. $tutorial->update();
  98. return redirect('maintenance/tutorial')->with('successTip','教程“'.$request->input('name').'”修改成功');
  99. }
  100. /**
  101. * Remove the specified resource from storage.
  102. *
  103. * @param int $id
  104. * @return \Illuminate\Http\Response
  105. */
  106. public function destroy($id)
  107. {
  108. if(!Gate::allows('教程-删除')){ return redirect(url('/')); }
  109. $tutorial=Tutorial::find($id);
  110. $tutorial->delete();
  111. TrixRichText::where('model_id',$id)->delete();
  112. $attachments = TrixAttachment::where('attachable_id', $id)->get();
  113. foreach ($attachments as $attachment){
  114. optional($attachment)->purge();
  115. }
  116. return ['success'=>true];
  117. }
  118. public function validator(Request $request){
  119. $validator=Validator::make($request->input(),[
  120. 'name'=>['required','max:50'],
  121. 'owner_id'=>['required','integer'],
  122. ],[
  123. 'required'=>':attribute 为必填项',
  124. 'max'=>':attribute 输入过长',
  125. ],[
  126. 'name'=>'标题',
  127. 'owner_id'=>'货主',
  128. ]);
  129. return $validator;
  130. }
  131. }