RonZ RonZ - 4 months ago 18
MySQL Question

Laravel 4.x, Eloquent multiple conditions

I've tried to this:

Product::where(['product_id' => $product->id, 'catalog_id' => $key])->first();


This isn't working at all. When I'm doing this:

Product:where('product_id', $product->id)->where('catalog_id', $key)->first();


It just works fine. I've searched in the documentation of Laravel,
and found nothing.

Is there any option to using the
where
function with an array in it ?

Answer

You need to use where() individually. If you want to dynamically building the query you can do something like:

$wheres = array('product_id' => $product->id, 'catalog_id' => $key);

$q = new Product;

foreach ( $wheres as $k => $v ) {
    $q = $q->where($k, $v); 
}

$products = $q->first();
Comments