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

Help to build a where clause #33

Closed jadersbr closed 3 years ago

jadersbr commented 3 years ago

Hi,

I am trying to generate the following result for a filter in a game query:

platforms = (18) & name ~ *"final fantasy"*;

I did this:

->where('platforms', '=', '('. $request->platform . ')')->where('name', '~', '*' . $request->game . '*')

But result was:

platforms = "(18)" & name ~ "*final fantasy*"

And did not work...

Could you help me achieve this result?

Regards.

marcreichel commented 3 years ago

Hi,

so what you want to achieve is a whereIn combined with whereLike.

The correct query would look something like the following:

Game::whereIn('platforms', [$request->platform])
    ->whereLike('name', '%' . $request->game . '%', false)
    ->get();

Please note: The third parameter (false) in the whereLike method makes the query case insensitive and the prefix/suffix % determine where the * should be placed. So "%final fantasy%" becomes *"final fantasy"* in your case.

Another possible notation would be:

Game::whereIn('platforms', [$request->platform])
    ->where('name', 'ilike', '%' . $request->game . '%')
    ->get();

Under the hood this just calls the upper example.

jadersbr commented 3 years ago

thank you very much, this solution worked fine!