2

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 ?

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
RonZ
  • 743
  • 1
  • 11
  • 31

4 Answers4

4

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();
Dave
  • 3,658
  • 1
  • 16
  • 9
4

In fact we were all wrong ;)

As of latest version of the framework you can do exactly what you wanted.

Check this commit and update Laravel if you need that feature.

https://github.com/laravel/framework/commit/87b267a232983abdac7c23c2dc6b1b270dd24b8a

Jarek Tkaczyk
  • 78,987
  • 25
  • 159
  • 157
  • 4 days ago though, should have been there since 4.1 at least. +1 for referencing something new and updating this question. – azngunit81 Jun 25 '14 at 21:39
2
Product::whereNested(function($query) use ($key, $product){
            $query->where('product_id', $product->id);
            $query->where('catalog_id', $key);
        })->get();
0

Laravel's wheres use an and condition by default:

$products = Product::where('this','=','that')->where('something','=','hello')->get();

is somewhat equivalent to:

SELECT * FROM products WHERE this = 'that' AND something = 'hello';

You simply chain the ->where() methods together. No need for an array.

If you want to use an or condition:

$products = Product::where('this','=','that')->orWhere('something','=','hello')->get();
Jake Wilson
  • 88,616
  • 93
  • 252
  • 370