簡體   English   中英

如何通過 id laravel 過濾 json api 響應

[英]How to filter json api response by id laravel

我正在調用端點以獲取返回大量數據的數據。 我需要將我需要的那些划分到我自己的類別中。 基本上,當我撥打電話時,我會收到此示例響應。

{
    "status": "success",
    "message": "bill categories retrieval successful",
    "data": [{
        "id": 1,
        "biller_code": "BIL099",
        "name": "MTN NIgeria",
        "default_commission": 0.03,
        "date_added": "2018-07-03T00:00:00Z",
        "country": "NG",
        "is_airtime": true,
        "biller_name": "AIRTIME",
        "item_code": "AT099",
        "short_name": "MTN",
        "fee": 0,
        "commission_on_fee": false,
        "label_name": "Mobile Number",
        "amount": 0
    }, {
        "id": 2,
        "biller_code": "BIL099",
        "name": "GLO Nigeria",
        "default_commission": 0.03,
        "date_added": "2018-07-03T00:00:00Z",
        "country": "NG",
        "is_airtime": true,
        "biller_name": "AIRTIME",
        "item_code": "AT099",
        "short_name": "GLO",
        "fee": 0,
        "commission_on_fee": false,
        "label_name": "Mobile Number",
        "amount": 0
    }, 
... //on and on
}

在我的代碼中,我有這個來過濾它,但它返回空......

public function billPower()
{
    $response = Http::withToken('FLWSECK_******-X')->get('https://baseUrl/v3/bill-categories', [

    ]);
    $collection = collect($response->json());
    $filtered = $collection->whereIn('id', [1, 2, 3, 4]);
    return $filtered->all();
}

如何在 laravel 中做到這一點?

你需要做:

$collection = collect($response->json()['data']);

那么你的過濾器應該可以工作

$filtered = $collection->whereIn('id', [1, 2, 3, 4]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM