belongsTo(GameType::class, 'game_type_id', 'id'); } //这个查不出来虚拟球员,所以没有用到 public function users() { return $this->belongsToMany(User::class, 'game_users', 'game_id', 'user_id'); } //关联发起人 public function creator_s() { return $this->belongsTo(User::class, 'creator', 'id'); } //关联比赛场地 public function course() { return $this->belongsTo(Course::class, 'course_id', 'id'); } public static function getGameInfo($id) { $info = self::where('games.id', $id) ->leftJoin('game_types', 'game_types.id', '=', 'games.game_type_id') ->leftJoin('courses', 'courses.id', '=', 'games.course_id') ->select('games.id as game_id', 'games.name', 'games.creator', 'begin_time', 'game_types.name as game_type', 'game_types.scale', 'courses.name as course_name', 'courses.img as course_avatar') ->first(); if(empty($info)){ return []; } $info = $info->toArray(); $info['actual_num'] = self::getActualCount($id); $timeParse = Carbon::parse($info['begin_time']); $info['begin_time'] = $timeParse->format('Y-m-d H:i'); $info['month'] = $timeParse->format('Y-m'); $info['date1'] = $timeParse->format('Y-m-d'); $info['date2'] = $timeParse->format('m-d'); $info['time1'] = $timeParse->format('m-d H:i'); $info['time2'] = $timeParse->format('H:i'); $info['week1'] = $timeParse->format('w'); switch ($info['week1']){ case 1: $week = 'Mon'; break; case 2: $week = 'Tue'; break; case 3: $week = 'Wed'; break; case 4: $week = 'Thu'; break; case 5: $week = 'Fri'; break; case 6: $week = 'Sat'; break; default: $week = 'Sun'; break; } $info['week2'] = $week; $info['for_human_read'] = $timeParse->diffForHumans(); $info['players'] = (new GameService())->getGameUsers($id); return $info; } //获取比赛的当前实际人数 public static function getActualCount($gameId) { $count = GameUser::query() ->where('game_id', $gameId) ->where('status', 1) ->whereNull('deleted_at') ->count('id'); return $count; } }