0

I have an array

$user_lgas = ['lga1', 'lga2', 'lga3', ...];

I want to write a scopeSearch to get records base on user lgas something like

$query->where('lga', $user_lgas[0])->orWhere('lga', $user_lgas[1])->orWhere('lga', $user_lgas[2]) ...

I want to generate this query dynamically from the array, but the logic is kind of complicated for me. Please any Help

Abdullah
  • 11
  • 5

2 Answers2

1

As per the Laravel Documentation you should be able to use whereIn() or whereNotIn() with an array.

$users = DB::table('users')
                    ->whereIn('id', array(1, 2, 3))->get();
 
$users = DB::table('users')
                    ->whereNotIn('id', array(1, 2, 3))->get();

So for your example

$query->whereIn('lga',$user_lgas)->get();

For more details see: Database: Query Builder

CloudTheWolf
  • 334
  • 2
  • 10
  • When someone asks a basic question in 2022, you can be sure that it is a duplicate question. Please flag/vote to close duplicates instead of answering. Answering duplicates prevents the [Roomba](https://stackoverflow.com/help/roomba) from doing its important job. – mickmackusa May 04 '22 at 09:49
0

You can just use a whereIn instead.

$query->whereIn('lga', $user_lgas)
Bernard Wiesner
  • 961
  • 6
  • 14
  • When someone asks a basic question in 2022, you can be sure that it is a duplicate question. Please flag/vote to close duplicates instead of answering. Answering duplicates prevents the [Roomba](https://stackoverflow.com/help/roomba) from doing its important job. – mickmackusa May 04 '22 at 09:49