TutorialController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. * @return \Illuminate\Http\Response
  26. */
  27. public function create()
  28. {
  29. $owners=Owner::get();
  30. return view('maintenance.tutorial.create',['owners'=>$owners]);
  31. }
  32. /**
  33. * Store a newly created resource in storage.
  34. *
  35. * @param \Illuminate\Http\Request $request
  36. * @return \Illuminate\Http\Response
  37. */
  38. public function store(Request $request)
  39. {
  40. if(!Gate::allows('教程-录入')){ return redirect(url('/')); }
  41. $this->validator($request)->validate();
  42. $tutorial=Tutorial::create(['owner_id'=>$request->input('owner_id'),'name'=>$request->input('name')]);
  43. $tutorial['tutorial-trixFields']=request('tutorial-trixFields');
  44. $tutorial['attachment-tutorial-trixFields']=request('attachment-tutorial-trixFields');
  45. $tutorial->save();
  46. return redirect('maintenance/tutorial')->with('successTip','新教程“'.$request->input('name').'”添加成功');
  47. }
  48. //展示教程内容
  49. public function showContent($id){
  50. if(!Gate::allows('教程-查询')){ return redirect(url('/')); }
  51. $tutorial=Tutorial::with('trixRichText')->find($id);
  52. if ($tutorial&&$tutorial->trixRichText)return ['success'=>'true','data'=>$tutorial->trixRichText->content];
  53. return ['success'=>'false'];
  54. }
  55. /**
  56. * Display the specified resource.
  57. *
  58. * @param int $id
  59. * @return \Illuminate\Http\Response
  60. */
  61. public function show($id)
  62. {
  63. }
  64. /**
  65. * Show the form for editing the specified resource.
  66. *
  67. * @param int $id
  68. * @return \Illuminate\Http\Response
  69. */
  70. public function edit($id)
  71. {
  72. if(!Gate::allows('教程-编辑')){ return redirect(url('/')); }
  73. $tutorial=Tutorial::with('trixRichText')->find($id);
  74. $owners=Owner::get();
  75. return view('maintenance.tutorial.edit',['owners'=>$owners,'tutorial'=>$tutorial]);
  76. }
  77. /**
  78. * Update the specified resource in storage.
  79. *
  80. * @param \Illuminate\Http\Request $request
  81. * @param int $id
  82. * @return \Illuminate\Http\Response
  83. */
  84. public function update(Request $request, $id)
  85. {
  86. if(!Gate::allows('教程-编辑')){ return redirect(url('/')); }
  87. $this->validator($request)->validate();
  88. $tutorial=Tutorial::find($id);
  89. $tutorial->owner_id=$request->input('owner_id');
  90. $tutorial->name=$request->input('name');
  91. $tutorial['tutorial-trixFields']=request('tutorial-trixFields');
  92. $tutorial['attachment-tutorial-trixFields']=request('attachment-tutorial-trixFields');
  93. $tutorial->update();
  94. return redirect('maintenance/tutorial')->with('successTip','教程“'.$request->input('name').'”修改成功');
  95. }
  96. /**
  97. * Remove the specified resource from storage.
  98. *
  99. * @param int $id
  100. * @return \Illuminate\Http\Response
  101. */
  102. public function destroy($id)
  103. {
  104. if(!Gate::allows('教程-删除')){ return redirect(url('/')); }
  105. $tutorial=Tutorial::find($id);
  106. $tutorial->delete();
  107. TrixRichText::where('model_id',$id)->delete();
  108. $attachments = TrixAttachment::where('attachable_id', $id)->get();
  109. foreach ($attachments as $attachment){
  110. optional($attachment)->purge();
  111. }
  112. return ['success'=>'true'];
  113. }
  114. public function validator(Request $request){
  115. $validator=Validator::make($request->input(),[
  116. 'name'=>['required','max:50'],
  117. 'owner_id'=>['required','integer'],
  118. ],[
  119. 'required'=>':attribute 为必填项',
  120. 'max'=>':attribute 输入过长',
  121. ],[
  122. 'name'=>'标题',
  123. 'owner_id'=>'货主',
  124. ]);
  125. return $validator;
  126. }
  127. }