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

Query filter on relationships #36

Closed MaximeGraindor closed 3 years ago

MaximeGraindor commented 3 years ago

Hello, first of all, thank you for this wrapper which allows me to work with IGDB more easily.

However, I have a little problem with the relations. I try to sort the games I get by genre but it doesn't work

Here is how I do it $games = IGDBGame::with(['platforms', 'cover', 'genres']); $games->whereHas('genres', function($query){ $query->where('name', 'platform'); }); I didn't find in your documentation something that does similar Do you have any idea how to filter a result via a query with your wrapper?

Thanks for everything!

marcreichel commented 3 years ago

Hi @MaximeGraindor,

thank you for your kind words.

Currently it is not possible to directly filter using the whereHas method. A workaround would be to first filter for games which have a genres and then query for genres with a specific name (or slug):

$games = IGDBGame::with(['platforms', 'cover', 'genres'])
    ->whereHas('genres')
    ->where('genres.slug', 'platform')
    ->get();

This way you only get games which have the "platform" genre.

But I think it would be a nice to have addition to have this implemented. I will look into implementing this. :+1:

Your other mentioned issue - to sort the games by genre - seems to be a problem with the IGDB API itself. Even the provided example on their documentation on sorting (sort release_dates.date desc; which would be ->orderBy('release_dates.date', 'desc') using this package) currently does not work (for me at least).

Feel free to reach out to them on their discord. They are very supportive.

MaximeGraindor commented 3 years ago

Thank you for your answer, I will look into it! 👌 Thank you!