guessing-miniprogram/app/Http/Resources/Api/UserGameResource.php

68 lines
2.4 KiB
PHP

<?php
namespace App\Http\Resources\Api;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Http\Response;
use App\Models\UserGame;
class UserGameResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'activity_id' => $this->activity_id,
'user_id' => $this->user_id,
'nick_name' => $this->whenLoaded('user', function () {
return $this->user?->nick_name ?? '';
}, ''),
'avatar' => $this->whenLoaded('user', function () {
return $this->user?->avatar ?? '';
}, ''),
'game_name' => $this->whenLoaded('game', function () {
return $this->game?->name ?? '';
}, ''),
'game_home_field' => $this->whenLoaded('game', function () {
return $this->game?->home_field ?? '';
}, ''),
'game_away' => $this->whenLoaded('game', function () {
return $this->game?->away ?? '';
}, ''),
'game_at' => $this->whenLoaded('game', function () {
return $this->game?->game_at ? $this->game->game_at->format('Y-m-d H:i:s'): '';
}),
'game_day' => $this->whenLoaded('game', function () {
return $this->game?->game_at ? $this->game->game_at->format('Y-m-d'): '';
}),
'score' => $this->score,
'status' => $this->whenLoaded('game', function () {
return ($this->game?->state ?? 0) < 2 ?'0':($this->is_right ? '1':'2');
}, '0'),
'history' => $this->whenLoaded('userActivity', function () {
$num = UserGame::whereHas('game', function($q){
return $q->where('state', 2);
})->where([
'user_id' => $this->user_id,
'activity_id' => $this->activity_id,
])->count();
if($num > 0) {
// if($this->userActivity->join_times > 0) {
return '最近他猜'.$num.'中'.($this->userActivity->right_times ?? 0);
}else{
return '';
}
}, ''),
];
}
public function with($request)
{
return ['code' => Response::HTTP_OK, 'message' => ''];
}
}