Laravel查找包含特定字符串值的项目

时间:2017-06-15 00:45:10

标签: php laravel laravel-5 eloquent backend

桌面上的一个字段包含一个具有游戏信息的对象(团队名称,团队得分等)。我的问题是如何检查某个商品是否包含特定的 competitorId 值?

表格字段值

[{
    "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", ▶"

2 个答案:

答案 0 :(得分:1)

您可以使用php函数in_array()搜索特定的字符串值

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

答案 1 :(得分:1)

使用$array = array(); $array['images']['93-8315415.jpg']['file_title']='93-8315415'; $array['images']['93-8315415.jpg']['file_date']='2'; $array['images']['94-1234.jpg']['file_title']='94-1234'; $array['images']['94-1234.jpg']['file_date']='3'; $array['images']['93-1138446.jpg']['file_title']='93-1138446'; $array['images']['93-1138446.jpg']['file_date']='1'; $array['images']['94-12345.jpg']['file_title']='94-12345'; $array['images']['94-12345.jpg']['file_date']='4'; // Ascending sort. echo "<pre>"; print_r(array_sort($array,'file_date')); // Descending sort. echo "<pre>"; print_r(array_sort($array,'file_date',SORT_DESC)); 使所有对象处于同一级别,然后使用flattenfilter对其进行过滤。

whereIn

更多详细信息可以查看laravel filterwhereIn