【问题标题】:Laravel: orWhere in a loopLaravel:或循环中的位置
【发布时间】:2015-02-03 14:54:19
【问题描述】:

我试图根据变量的数量在查询中获取一些 orWhere 子句,但我现在得到的查询结果不是我需要的。

$names = Input::get('name');
$query = DB::table('table')
->where('name', '=', 'something');
foreach ($naam_categorie as $naam){
    $query->orWhere('id',$naam));
}

$query->update(array('value' => 1));

我将在特定的 where 子句中更新为 value = 1。如何将剩余的值设为 0?

这里到底出了什么问题?

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    您需要使用get()返回值,而不是查询构建器实例:

    $result = $query->get();
    var_dump($result);
    

    关于输入,我会这样做:

    $names = Input::get('name');
    $query = DB::table('table');
    foreach($names as $name){
       $query->orWhere('id', $name);
    }
    $result = $query->get();
    

    编辑

    对于更新最简单的事情可能是首先将所有设置为0,然后将匹配到1的那些:

    DB::table('table')->update(array('value' => 0));
    
    $names = Input::get('name');
    $query = DB::table('table');
    foreach($names as $name){
       $query->orWhere('id', $name);
    }
    $query->update(array('value' => 1));
    

    【讨论】:

    • 输入的东西是,它包含多个值,它取决于$x 变量的数量在刀片中,名称是:name[]跨度>
    • 我知道了,因此是foreach$x 变量是什么?不就是数组中名字的个数吗?
    • 是的。 $x = 值的数量。
    • 那么你实际上不应该需要那个。试试我的代码;)
    • 是的,您可以删除第一个 where。查询生成器足够聪明,只能将 ORAND 运算符放在两个 where 之间;)
    猜你喜欢
    • 1970-01-01
    • 2013-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-07
    • 1970-01-01
    • 2020-04-25
    相关资源
    最近更新 更多