Skip to content
Advertisement

How to make sql foreach loop by Laravel Query builder?

I have an array. Can I send the values ​​in an array with one query and get a separate response for all arrays …?

$regions:  array;

$result is laravel query result

[‘index’] is index from SQL

$data['persons'] = $reslut['index']['persons'];
$data['companies'] = $reslut['index']['companies'];

If I am sending an array to sql and can it return a separate value for each value? That is, the foreach loop should use sql and return the results to individual variables. Can I write in any of the indexes?

Advertisement

Answer

You can query array data with a whereIn statement:

$results = Model::whereIn('your_field', $array)->get()

This will respond with a collection that contains multiple results. You can loop over them with for($results as $result){} or count them with $results->count()

.. But i’m not complete sure I did understand your question correct πŸ˜‰

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement