marcreichel/igdb-laravel

(Question) Game Characters

Closed this issue · 6 comments

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.

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

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.

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

@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();
}

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