0

One field on a table contains an object that has the information of a game (both team names, both team scores etc.). My question is how can I check if an item contains specific competitorId value?

Table Field Value

[{
    "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": "",
}]

Model Method

public static function getTeamWinLossRecord($competitionId, $teamId) {
    $data = self::select('competitors')
            ->where('competitionId', $competitionId)
            ->get()
            ->map(function($item, $teamId){
                return $item->competitors;
            });
    dd($data);
}

Returned 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", ▶"

2 Answers 2

1

Use flatten to make all objects in same level, then filter it using filter or whereIn.

$newData = $data->flatten()->filter(function($val) {
    return $val->competitorId === "someIdid"
)}

More details can check on laravel filter and whereIn

Sign up to request clarification or add additional context in comments.

Comments

1

You can use a php function in_array() for search a specific string value

Font: https://secure.php.net/manual/en/function.in-array.php

1 Comment

Going off your answer, the Collection class has a contains method that does essentially the same thing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.