guard()->user(); $list = Paper::with(['examination']) ->where('employee_id', $user->id) ->whereHas('examination', fn ($q) => $q->where('exam_status', ExamStatus::Published)) ->orderBy('created_at', 'desc') ->paginate($request->input('per_page')); return TrainPaperResource::collection($list); } public function show($id) { $user = $this->guard()->user(); $info = Paper::with(['examination'])->where('employee_id', $user->id)->findOrFail($id); return TrainPaperResource::make($info); } public function answer($id, Request $request, PaperService $service) { $request->validate([ 'answers' => ['required', 'array'], ]); $user = $this->guard()->user(); $info = Paper::with(['examination'])->where('employee_id', $user->id)->findOrFail($id); try { DB::beginTransaction(); if (! $service->answer($info, $request->input('answers'))) { throw new RuntimeException($service->getError()); } DB::commit(); return TrainPaperResource::make($info); } catch (\Exception $e) { DB::rollBack(); throw new RuntimeException($e->getMessage()); } } }