Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help to build a where clause #33

Closed
jadersbr opened this issue Apr 9, 2021 · 2 comments
Closed

Help to build a where clause #33

jadersbr opened this issue Apr 9, 2021 · 2 comments

Comments

@jadersbr
Copy link

jadersbr commented Apr 9, 2021

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
Copy link
Owner

marcreichel commented Apr 10, 2021

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
Copy link
Author

thank you very much, this solution worked fine!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Development

No branches or pull requests

2 participants