表格中的一個字段包含一個包含遊戲信息(包括團隊名稱,團隊分數等)的對象。我的問題是如何檢查一個項目是否包含具體的competitorId的值?Laravel找到一個包含特定字符串值的項目
表字段值
[{
"competitorType": "TEAM",
"competitorName": "Team A",
"competitorId": 40000,
"linkDetailCompetitor": "\/v1\/basketball\/teams\/40000",
"scoreString": "85",
"scoreSecondaryString": "",
"completionStatus": "COMPLETE",
"resultPlacing": 0,
"isDrawn": 0,
"isHomeCompetitor": 0,
"teamId": 40000,
"teamName": "Team A",
"teamNameInternational": "",
"teamNickname": "A",
"teamNicknameInternational": "",
"teamCode": "TA",
"teamCodeInternational": "",
"website": "",
"internationalReference": "",
"externalId": "71",
"clubId": 1067,
"clubName": "Team A",
"clubNameInternational": "",
}, {
"competitorType": "TEAM",
"competitorName": "Team B",
"competitorId": 40001,
"linkDetailCompetitor": "\/v1\/basketball\/teams\/40001",
"scoreString": "89",
"scoreSecondaryString": "",
"completionStatus": "COMPLETE",
"resultPlacing": 1,
"isDrawn": 0,
"isHomeCompetitor": 1,
"teamId": 40001,
"teamName": "Team B",
"teamNameInternational": "",
"teamNickname": "B",
"teamNicknameInternational": "",
"teamCode": "TB",
"teamCodeInternational": "",
"website": "",
"internationalReference": "",
"externalId": "72",
"clubId": 1095,
"clubName": "Team B",
"clubNameInternational": "",
}]
模型法
public static function getTeamWinLossRecord($competitionId, $teamId) {
$data = self::select('competitors')
->where('competitionId', $competitionId)
->get()
->map(function($item, $teamId){
return $item->competitors;
});
dd($data);
}
返回的數據
Collection {#396 ▼
#items: array:120 [▼
0 => "[{"competitorType":"TEAM","competitorName":"Team A","competitorId":40000,"linkDetailCompetitor":"\/v1\/basketball\/teams\/40000","scoreString":"95","score ▶"
1 => "[{"competitorType":"TEAM","competitorName":"Team C","competitorId":40001,"linkDetailCompetitor":"\/v1\/basketball\/teams\/40001","scoreString":"84", ▶"
去了你的答案,集合類有一個contains方法,基本上做同樣的事情。 – btl