amilajack
amilajack

Reputation: 127

Eloquent where() statements

I'm trying make a function that returns false if there is no inventory available. I need it to search columns, defined by the user, for a certain field. To do this I need to make the following select statement in Laravel using eloquent:

find an the column named "type", find the value "iphone 4s" where the "new" column is greater than zero AND where the color field is greater than zero AND (etc).

I attempted it with the code below. I tried to make the query search fail on purpose just to test if it works but it kept on returning the row. My other question is how would I return it as a boolean?

// Check the inventory to see if in stock
    $instock = DB::table('Inventory')
            ->where('type', '=', $id)
            ->where(Input::get('condition'), '>', 100000)
            ->where(Input::get('color'), '>', 100000)
            ->get();
    dd($instock);

Here's the array it returns, in case if it helps you. As you can see, the values are well below 100000.

array(1) {
  [0]=>
  object(stdClass)#240 (9) {
    ["type"]=>
    string(8) "iphone4s"
    ["new"]=>
    string(1) "2"
    ["good"]=>
    string(1) "2"
    ["used"]=>
    string(1) "1"
    ["sprint"]=>
    string(1) "4"
    ["att"]=>
    string(1) "1"
    ["verizon"]=>
    string(1) "5"
    ["white"]=>
    string(1) "1"
    ["black"]=>
    string(1) "3"
  }
}

Upvotes: 2

Views: 7714

Answers (2)

lowerends
lowerends

Reputation: 5267

You need to rewrite your query as follows:

$instock = DB::table('Inventory')
    ->where('type', '=', $id)
    ->where('condition', '>', Input::get('condition'))
    ->where('color', '>', Input::get('color'))
    ->get();

Upvotes: 3

jmadsen
jmadsen

Reputation: 3675

One option would be to use ->count() to get the number of rows, then just:

if ($instock)

since any value > 0 will be "true"

Upvotes: 0

Related Questions