RonZ RonZ - 1 year ago 106
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
function with an array in it ?

Answer Source

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();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download