where在过滤laravel查询时被忽略

时间:2018-05-17 09:00:00

标签: php laravel

我正在尝试过滤数据库查询,我想知道是否可以使用whereIn来执行此操作?目前它被忽略了。

第一个查询工作正常:

      $cosmeticTestAllData = DB::table('table')
            ->whereIn('part_number', ['P1', 'P2', 'P3', 'P4', 'P5', 'P6'])
            ->get();

示例结果:

Array    (
[0] => stdClass Object
    (
        [part_number] => P1
        [status] => FAIL
        [shipment_channel] => DP
        [created_at] => 2017-01-24 10:25:21
    )

[1] => stdClass Object
    (
        [part_number] => P2
        [status] => PASS
        [shipment_channel] => DP
        [created_at] => 2018-01-24 10:25:21
    )

[2] => stdClass Object
    (
        [part_number] => P2
        [status] => FAIL
        [shipment_channel] => DP
        [created_at] => 2018-01-24 10:25:21
    )

 )

此查询正确过滤了where子句,但忽略了whereIn

        $fullCount = $cosmeticTestAllData;
        $fullCount->where('created_at', '>', '2018-01-01 00:00:00');
        $fullCount->whereIn('shipment_channel', ['ANYTHING']);
        $fullCount->whereIn('status', ['ANYTHING')];
        echo '<pre>';
        print_r($fullCount->all());die;

结果:

Array    (

[0] => stdClass Object
    (
        [part_number] => P2
        [status] => PASS
        [shipment_channel] => DP
        [created_at] => 2018-01-24 10:25:21
    )

[1] => stdClass Object
    (
        [part_number] => P2
        [status] => FAIL
        [shipment_channel] => DP
        [created_at] => 2018-01-24 10:25:21
    )

 )

3 个答案:

答案 0 :(得分:0)

而不是whereIn,你应该在第二个查询中尝试orWhere。

答案 1 :(得分:0)

我认为(?)你想要这样的东西:

$rows = DB::table('table')
  ->where('created_at', '>', '2018-01-01 00:00:00')
  ->whereIn('part_number', ['P1', 'P2', 'P3', 'P4', 'P5', 'P6'], 'AND')
  ->whereIn('shipment_channel', ['ANYTHING'], 'AND')
  ->whereIn('status', ['ANYTHING'], 'AND')
  ->get();

请注意whereIn接受第三个(和第四个)参数:

/**
 * Add a "where in" clause to the query.
 *
 * @param  string  $column
 * @param  mixed   $values
 * @param  string  $boolean
 * @param  bool    $not
 * @return \Illuminate\Database\Query\Builder|static
 */
public function whereIn($column, $values, $boolean = 'and', $not = false)
{
    $type = $not ? 'NotIn' : 'In';

    // If the value of the where in clause is actually a Closure, we will assume that
    // the developer is using a full sub-select for this "in" statement, and will
    // execute those Closures, then we can re-construct the entire sub-selects.
    if ($values instanceof Closure)
    {
        return $this->whereInSub($column, $values, $boolean, $not);
    }

    $this->wheres[] = compact('type', 'column', 'values', 'boolean');

    $this->bindings = array_merge($this->bindings, $values);

    return $this;
}

老实说,从问题中你想做什么并不完全清楚。

答案 2 :(得分:0)

您似乎试图将集合视为查询构建器。但是,大多数收集操作都不会改变收集。将您的代码更改为:

$fullCount = $cosmeticTestAllData
       ->where('created_at', '>', '2018-01-01 00:00:00');
       ->whereIn('shipment_channel', ['ANYTHING'])
        ->whereIn('status', ['ANYTHING' ]);
    echo '<pre>';
    print_r($fullCount->all());die;

这样您就可以将每个操作的结果链接到下一个操作。

或者,您可以在执行查询

之前在查询构建器上执行这些操作