input('activity_id', 0); $games = ActivityGame::with(['logs'=> function($q){ $q->where('user_id', auth('api')->user()?->id ?? 0); }]) ->where('activity_id', $activityId)->show()->sort() ->simplePaginate($request->query('per_page', 20)); return $this->success(['games'=>ActivityGameResource::collection($games)->resolve()]); } public function latestGame(Request $request) { //获取最新的活动; $activity = Activity::show()->sort()->first(); $game = $activity?->games()->show() ->whereDate('game_at', now()) ->where('game_at', '>', now()) ->where('state', 1) ->orderBy('game_at', 'asc')->first(); //若已经没有最新的活动了, 则拿取当天最后一个 if(!$game){ $game = $activity?->games()->show() ->whereDate('game_at', now()) ->orderBy('game_at', 'desc')->first(); } if($game){ $game->load(['logs'=> function($q){ $q->where('user_id', auth('api')->user()?->id ?? 0); }]); return ActivityGameResource::make($game); } return $this->success(); } public function joinGame(ActivityGame $game, JoinGameRequest $request) { $res = $this->service->join($game, $request->user(), $request->input('score')); if($res['status']){ return $this->success(null, '竞猜成功'); }else{ return $this->error($res['message']); } } }