【问题标题】:whereIn being ignored when filtering a laravel query过滤 laravel 查询时忽略 whereIn
【发布时间】:2018-05-17 08:59:59
【问题描述】:

我正在尝试过滤数据库查询,我想知道是否可以使用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
    )

 )

【问题讨论】:

  • 使用get() 而不是all()
  • 当我使用get() 时出现以下错误...Missing argument 1 for Illuminate\Support\Collection::get(),我假设get() 仅用于数据库查询
  • 为什么不将 whereIn 的第二个参数作为数组传递
  • 抱歉,我的问题打错了,现在我更新代码,因为在实际代码中,即使将它作为数组,问题仍然存在。
  • 您检查过 Eloquent 实际生产的产品吗?尝试使用dd($fullCount-&gt;toSql()) 来检查正在生成的 sql

标签: php laravel


【解决方案1】:

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

$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;

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

或者,您可以在执行查询之前在查询生成器上执行这些操作

【讨论】:

  • 这是解决方案,我没有将它们链接起来,因为代码中有更多我不想包含的条件,因为它会使问题复杂化。但是,似乎我将不得不以某种方式解决这个问题。一旦我将它们直接链接起来,它就完美过滤了,谢谢!我不想在查询生成器上执行它们,因为过滤正在循环,我不想在循环中查询数据库 100 次
【解决方案2】:

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

【讨论】:

  • 谢谢,我的代码中有一个错字,我没有表明我正在寻找在第二个查询中查询数组。我现在已经更新了。无论如何我都会尝试这个并标记为正确,如果它有效。我试图避免这种情况,因为我虽然 whereIn() 可能就足够了
【解决方案3】:

我假设(?)你想要这样的东西:

$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;
}

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-18
    • 2011-12-15
    • 1970-01-01
    • 2018-07-15
    相关资源
    最近更新 更多