mercredi 14 octobre 2015

Add 'where' in query with foreach or something similar

this is my problem.

product_field_glue table:

id | field_id | value
----------------------
1  | 50       | blue
2  | 51       | wood
3  | 50       | red
4  | 35       | car

I need to search products by this fields according _GET params

/?field[50][]=blue&field[35][]=car

this should return all blue cars

$result = Products::select('products.*)
 ->leftJoin('product_field_glue', function ($join) {
      $join->on('product_field_glue.product_code', '=', 'products.product_code');
        })
 ->where(function($query) use ($id, $value){
            $query->where('product_fields.field_id', $id);
            $query->where('product_fields.value', $value);
        })

This is modified (part of) query. As you can see I need to set 'where' for all _GET['field'] and $key=>$value of GET parameter need to be in grouped where. Maybe there is better solution for this kind of search which you can recommend.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire