0

I want to use the array $aqui inside where clause, but I don't know how to do it. I know how to put multiple values inside where clause, but I don't know about arrays.

$aqui[1] = 35;
$aqui[2] = 67;
$aqui[3] = 44;
$aqui[4] = 12;
$aqui[5] = 9;
//and goes...

$caraio = DB::connection('mysql')
->table('users')
->select('name')
->where( /* where fields are equal to $aqui */ )
->value('name');
7
  • Why not use Eloquent model? Commented Mar 8, 2020 at 16:18
  • Because i find it confusing and harder to learn and everything i did until now is using Query Builder. Commented Mar 8, 2020 at 16:21
  • 2
    That's fine, but you can try. I am saying this because they make your work a lot easier. Otherwise, you have to write raw queries yourself. Commented Mar 8, 2020 at 16:22
  • are you looking for ->whereIn('aqui', $aqui)? (guessing here that the attribute you want to filter by is called aqui as well - if not, adjust accordingly Commented Mar 8, 2020 at 16:56
  • 2
    Because i find it confusing So practice? It’s more conventional to use models in Laravel (and most other web frameworks) rather than manually writing queries all over your application. Commented Mar 8, 2020 at 18:39

1 Answer 1

0

Just use:

$aqui[1] = 35;
$aqui[2] = 67;
$aqui[3] = 44;
$aqui[4] = 12;
$aqui[5] = 9;
//and goes...

$caraio = DB::connection('mysql')
->table('users')
->select('name')
->whereIn('attributeName', $aqui)
->value('name');

Obviously you want to replace attributeName with the name of the attribute that you want to compare values against.

Read more about it here.

Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.