marcreichel / igdb-laravel

Simplify the integration of the IGDB API into your Laravel app. Including IGDB webhook support.
https://marcreichel.dev/docs/igdb-laravel
MIT License
107 stars 22 forks source link

(Question) Game Characters #11

Closed HDVinnie closed 5 years ago

HDVinnie commented 5 years ago

How does one go about getting 6 game characters from a game?

Im using at moment:

if ($torrent->category->game_meta) {
            $meta =  Game::with(['cover' => ['url', 'image_id'], 'artworks' => ['url', 'image_id'], 'genres' => ['name']])->find($torrent->igdb);
}

What I would like to do in my view is display 6 character names and mugshots.

This is what my view is at the moment. https://pastebin.com/cZ47Sbpd

Thanks for the great package.

marcreichel commented 5 years ago

If I understand you right, just extend your with clause to also fetch characters?

And then in your yiew you could do @foreach($meta->characters->take(6) as $character) ... @endforeach

HDVinnie commented 5 years ago

Yeah thats what I though however when I add ->with(['characters') to the query I then get this error.

Trying to get property 'artworks' of non-object (View: /var/www/html/resources/views/torrent/partials/game_meta.blade.php) 

Artwork works fine when query is

if ($torrent->category->game_meta) {
            $meta =  Game::with(['cover' => ['url', 'image_id'], 'artworks' => ['url', 'image_id'], 'genres' => ['name']])->find($torrent->igdb);
}

However it breaks when changing it to

if ($torrent->category->game_meta) {
            $meta =  Game::with(['characters'])->with(['cover' => ['url', 'image_id'], 'artworks' => ['url', 'image_id'], 'genres' => ['name']])->find($torrent->igdb);
}

or

        if ($torrent->category->game_meta) {
            $meta =  Game::with(['cover' => ['url', 'image_id'], 'artworks' => ['url', 'image_id'], 'characters' ,'genres' => ['name']])->find($torrent->igdb);
        }

And thats without changing anything in the view.

marcreichel commented 5 years ago

Hm. Very strange. I‘ll investigate this behavior and will get in touch with you tomorrow again.

HDVinnie commented 5 years ago

Thanks @marcreichel

marcreichel commented 5 years ago

@HDVinnie So. The problem is: Games do not have the characters listed. That's why you cannot extend your requests for characters. I don't really know why Games do not have this field/relationship to be honest. Maybe it's a bug 🤷‍♂ So as a workaround you need to fetch the characters separately using something like this:

if ($torrent->category->game_meta) {
    $meta =  Game::with(['cover' => ['url', 'image_id'], 'artworks' => ['url', 'image_id'], 'characters' ,'genres' => ['name']])->find($torrent->igdb);
    $characters = Character::whereIn('games', [$torrent->igdb])->take(6)->get();
}
HDVinnie commented 5 years ago

Very strange indeed there is no relation. IGDB API is weird though in my opinion. Thanks for the help.