【发布时间】:2018-01-15 14:11:41
【问题描述】:
我正在构建一个 REST API,我需要根据未包含在 EventScores 表中的某个 EVENT 返回 TEAMS。 我遇到的问题在于过滤。如果 $teams 被“过滤器”修改,该方法返回如下内容:
{
"1": {
"id": 6,
"name": "sunt",
"ath1": "Prof. Jarvis Rutherford",
"ath2": "Joey Gutmann",
"box": "ut",
"category_id": 1,
"totalScore": 6,
"created_at": "2018-01-11 18:22:13",
"updated_at": "2018-01-11 19:47:21"
}}
否则,它会返回一个 ARRAY,这是我需要在前端制作的东西。这是我需要的样本:
[
{
"id": 5,
"name": "eaque",
"ath1": "Prof. Trever Grady",
"ath2": "Angel Walsh",
"box": "voluptatem",
"category_id": 1,
"totalScore": 15,
"created_at": "2018-01-11 18:22:13",
"updated_at": "2018-01-11 19:47:21"
}]
这就是方法:
public function eventTeams($category_id, $event_id)
{
$teams = Team::where('category_id', $category_id)->get();
$eventScores = EventScores::where('event_id','=', $event_id)->get();
$teams = $teams->filter(function($team) use($eventScores)
{
if($eventScores->contains('team_id', $team->id)){
return false;
}else {
return true;
}
});
return response($teams, 200);
}
我试过这个:$teamsArr = $teams->toArray() 并返回 $teamsArr 但它不起作用。
【问题讨论】:
标签: php arrays laravel rest collections