jeudi 25 mai 2017

Issue with Laravel 5.4 where collection method

I have this piece of code:

if ($response && $response->getStatusCode() == 200) {
        $this->ticker = collect(collect(json_decode($response->getBody()->getContents(), true))->get('payload'));

        Log::info(json_encode($this->ticker));

        switch ($book):
            case 'btc_mxn':
                return $this->ticker->whereStrict('book', 'btc_mxn');
            case 'eth_mxn':
                return $this->ticker->whereStrict('book', 'eth_mxn');
            case 'all':    
                return $this->ticker;
            default:
                throw new ValidationHttpException(['Invalid book (9002)']);
        endswitch;
    }

The $response part is not important, only the $this->ticker part. This gets an API response which contains an array of objects. The objects contain a book field which I care about. The json looks something like this:

[{"high":"59999.00","last":"53021.11","created_at":"2017-05-25T23:16:44+00:00","book":"btc_mxn","volume":"1313.28966742","vwap":"55354.76622471","low":"50000.00","ask":"53998.92","bid":"53021.11"},{"high":"4699.00","last":"4102.00","created_at":"2017-05-25T23:16:44+00:00","book":"eth_mxn","volume":"7162.16835199","vwap":"4322.60134630","low":"3900.00","ask":"4102.00","bid":"4100.00"},{"high":"0.00012498","last":"0.00010700","created_at":"2017-05-25T23:16:44+00:00","book":"xrp_btc","volume":"17584.07258163","vwap":"0.00010897","low":"0.00009500","ask":"0.00011990","bid":"0.00010100"},{"high":"7.10","last":"6.05","created_at":"2017-05-25T23:16:44+00:00","book":"xrp_mxn","volume":"1015137.88406527","vwap":"6.28004670","low":"5.50","ask":"6.05","bid":"5.85"},{"high":"0.08197000","last":"0.07800000","created_at":"2017-05-25T23:16:44+00:00","book":"eth_btc","volume":"73.29999906","vwap":"0.07656212","low":"0.07250000","ask":"0.07800000","bid":"0.07600000"}] 

The issue comes when I switch the $book. If $book = 'btc_mxn' I get an array of objects:

[
  {
    "high": "59999.00",
    "last": "53000.00",
    "created_at": "2017-05-25T23:23:29+00:00",
    "book": "btc_mxn",
    "volume": "1316.43950673",
    "vwap": "55323.60047189",
    "low": "50000.00",
    "ask": "53000.00",
    "bid": "52001.00"
  }
]

However, if $book = 'eth_mxn' I get an object!

{
  "1": {
    "high": "4699.00",
    "last": "4025.97",
    "created_at": "2017-05-25T23:24:18+00:00",
    "book": "eth_mxn",
    "volume": "7360.11920724",
    "vwap": "4310.48584845",
    "low": "3900.00",
    "ask": "4026.00",
    "bid": "4000.01"
  }
}

The same happens pretty much to any other book key which is not 'btc_mxn'.

I fix the issue by adding a call to the first() method, however this is very strange. Do someone has any idea about whats going on here?

Thanks.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2s0SLKt
via IFTTT

Aucun commentaire:

Enregistrer un commentaire