marcreichel/igdb-laravel

Help to build a where clause

jadersbr opened this issue · 2 comments

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.

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.

thank you very much, this solution worked fine!