2

I'am trying to implement caching into my Laravel 5 API, but I'm having trouble. I'am getting this error right now:

ErrorException in MedalController.php line 19:
Undefined property: GuzzleHttp\Psr7\Response::$Results

Can anyone figure out why this is not getting my header? I have never really used caching before, so I'am probably missing something here

This is how I'am calling and getting my medal count for each player in a Halo 5 game:

GetDataController:

class GetDataController extends Controller {


    /**
     * Fetch a Players Arena Stats
     *
     * @param $gamertag
     * @return mixed
     */
    public function getPlayerArenaStats($gamertag) {

        $client = new GuzzleHttp\Client();

        $baseURL = 'https://www.haloapi.com/stats/h5/servicerecords/arena?players=' . $gamertag;

        $res = $client->request('GET', $baseURL, [
            'headers' => [
                'Ocp-Apim-Subscription-Key' => env('Ocp-Apim-Subscription-Key')
            ]
        ]);

        Cache::put('stats', $res, 10);

        if ($res->getStatusCode() == 200) {
            return $result = json_decode($res->getBody());
        } elseif ($res->getStatusCode() == 404) {
            return $result = redirect()->route('/');
        }

        return $res;
    }

}

My MedalControler which calls the header and tries to get all the medals for a player:

class MedalController extends Controller {

    /**
     * Get a Players Arena Medals
     *
     * @param $playerArenaMedalStats
     * @return mixed
     */
    public function getArenaMedals($playerArenaMedalStats) {


        $results = collect($playerArenaMedalStats->Results[0]->Result->ArenaStats->MedalAwards);

        $array = $results->sortByDesc('Count')->map(function ($item, $key) {
            return [
                'MedalId' => $item->MedalId,
                'Count' => $item->Count,
            ];
        });

        return $array;
    }


}

And this is how how get, decode and return medals to view:

class StatsController extends Controller {


    /**
     * Return all Stats for a particular player
     * 
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request) {

        if (Cache::has('stats')) {
            $playerArenaMedalStats = Cache::get('stats');
            $playerArenaMedalStatsArray = app('App\Http\Controllers\MedalController')->getArenaMedals($playerArenaMedalStats);
            $arenaMedals = json_decode($playerArenaMedalStatsArray, true);
        } else {
            app('App\Http\Controllers\GetData\GetDataController')->getPlayerArenaStats($gamertag);
        }

      // More stuff here, shortened for simplicicty

      return view('player.stats')->with('arenaMedals', $arenaMedals);

    }


}
David
  • 1,987
  • 5
  • 33
  • 65

2 Answers2

1

As Ravisha Hesh said, I just had to change the cache in my getPlayerArenaStats to this:

 Cache::put('stats', json_decode($res->getBody()), 10);

I had to json_decode it first

David
  • 1,987
  • 5
  • 33
  • 65
0

It's return $res at the end of your getPlayerArenaStats method. It returns GuzzleHttp\Psr7\Response and you are trying to get $playerArenaMedalStats->Results[0] in your getArenaMedals method on MedalController

Ravisha Hesh
  • 1,504
  • 13
  • 19
  • If you are saying to remove *return $res*, then I did, and its still giving me same error – David May 16 '16 at 06:28
  • 1
    Sorry my bad, that's not the issue, in "getPlayerArenaStats" method on "GetDataController" you put GuzzleHttp\Psr7\Response into Cache using "Cache::put('stats', $res, 10);", then in StatsController" you get it to a variable "$playerArenaMedalStats = Cache::get('stats');". Then you pass it into "getArenaMedals" method. In getArenaMedals you call "$playerArenaMedalStats->Results[0]". That's the issue. So you have to put Json decode $res Cache in GetData Controller. `Cache::put('stats', json_decode($res->getBody()), 10); ` "return $res" also throws error if you get response codes like 500 – Ravisha Hesh May 16 '16 at 15:38
  • Yes, that works. Now I the data is caching without errors! – David May 16 '16 at 16:29
  • I just want to ask how caching works though. So does it mean each time I load up a users profile, it doesnt update the "Medals" for 10 minutes, until the cache is over? – David May 16 '16 at 16:30
  • I haven't much experiance with `Cache`, but I think that's the idea – Ravisha Hesh May 16 '16 at 16:44