Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

comms_express's avatar

Using an array in an SQL query

So i have this form where a user can search for product data and can filter it by ProductGroup.

The user has to be able to select more than one ProductGroup. How would i be able to add this to query?

Example

The data passed through from the form is this

        $searchData = [
            'product_group' =>[
        0=>'test1',
        1=>'test2',
        2=>'test3'
        ]
        ];

I have a query at the moment which check that the passed through values have values before adding them to the query.

My query at the moment is

 $results = StockProfitReportView::
    when(count($searchData['product_group']) > 0, function($query) use($searchData) {
             return $query->where('ProductGroup', $searchData['product_group']);
            })->get();

So how can i do it so it add a 'OR' line for every product group so the final query will look a little bit like

$results = StockProfitReportView::
    ->where('ProductGroup', 'test1')
        ->orWhere('ProductGroup', 'test2')
        ->orWhere('ProductGroup', 'test3')
    ->get();

If i have not explained this very well please let me know and i will try to clear it up.

Thank you in advance

0 likes
5 replies
realrandyallen's avatar
Level 44

Use a whereIn

return $query->whereIn('ProductGroup', $searchData['product_group']);
1 like
bitcoinboy's avatar
$results = StockProfitReportView::
    ->whereIn('ProductGroup',  $searchData['product_group'])
    ->get();
jlrdw's avatar

I thought you would have needed an array like

$somearray ['test1', 'test2', 'test3']

Didn't realize wherein worked also with an associative array.

Nice to know.

Please or to participate in this conversation.