Eloquent where()语句

时间:2014-07-29 00:56:12

标签: mysql sql laravel laravel-4 eloquent

如果没有可用的广告资源,我尝试创建一个返回false的函数。我需要它来搜索用户定义的列,用于某个字段。为此,我需要使用eloquent在Laravel中进行以下select语句:

找到一个名为&#34的列;键入",找到值" iphone 4s" " new"列大于零 AND ,其中色域大于零 AND (等)。

我尝试使用下面的代码。我试图使查询搜索失败,只是为了测试它是否有效,但它一直在返回行。我的另一个问题是如何将其作为布尔值返回?

// 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);

这是它返回的数组,以防它对你有帮助。如您所见,这些值远低于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"
  }
}

2 个答案:

答案 0 :(得分:2)

您需要按如下方式重写查询:

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

答案 1 :(得分:0)

一种选择是使用 - > count()来获取行数,然后只需:

if($ instock)

因为任何值> 0将是" true"