【问题标题】:Eloquent where() statements雄辩的 where() 语句
【发布时间】:2014-09-20 07:34:15
【问题描述】:

如果没有可用的库存,我正在尝试创建一个返回 false 的函数。我需要它来搜索由用户定义的特定字段的列。为此,我需要在 Laravel 中使用 eloquent 进行以下选择语句:

找到名为“type”的列,找到值“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"
  }
}

【问题讨论】:

    标签: mysql sql laravel laravel-4 eloquent


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      如果($库存)

      因为任何值 > 0 都将是“真”

      【讨论】:

      • 我不确定你的意思。你能给我看一个简短的例子吗?
      • (我是 Laravel 和一般编程的新手)抱歉我缺乏知识
      • 代替 ->get(),使用 ->count() 。签入文档,你会看到它
      猜你喜欢
      • 2017-10-18
      • 1970-01-01
      • 2014-01-03
      • 2017-08-31
      • 2020-07-22
      • 1970-01-01
      • 2015-05-17
      • 1970-01-01
      • 2017-09-15
      相关资源
      最近更新 更多