Jurchello
Jurchello

Reputation: 33

How to generate a query to db if several "where" are added in the loop?

I have a method with two incoming arrays. The first array $columns[] contains columns names. The second one $filters[] contains search filters. How can I sort through all the values of these columns using only one search query? I can not figure out how this is done through "Eloquent". I want generate something like this:

$data::where($columns[0], '=', $filters[0])
            ->where($columns[1], '=', $filters[1])
            ...
            ->where($columns[n], '=', $filters[n])

Upvotes: 0

Views: 33

Answers (2)

Vishnu M Raveendran
Vishnu M Raveendran

Reputation: 411

Try this:

 $data = new Model::query();
    for($i=0; $i<count($columns); $i++)
    {
       $data->where($columns[$i], $filter([$i]));
    }

  $res =  $data->get();

sorry if I got your question wrong

Upvotes: 1

Niklesh Raut
Niklesh Raut

Reputation: 34914

You can use any loop

$query = new Model;
$count = count($columns);
for($i=0;$<$count;$i++){
   $query->where($columns[$i], '=', $filters[$i]);
}
$result = $query->get();

Upvotes: 1

Related Questions