在laravel中使用orWhere会出错

时间:2016-07-24 19:09:02

标签: mysql laravel laravel-5 eloquent

我无法使用或在哪里,我不知道原因。

我的目标是拉amount_invoice=0 OR total_mileage=0。如果我仅使用WHERE子句,则查询有效。当我添加orWhere时,我的查询失败。

此查询有效:

$deliveries = $this->instance->delivery
        ->where('amount_invoice', 0);
        //->where('total_mileage', 0);
        //->where('amount_invoice', 0)->where('total_mileage',0);                                
        //->where('amount_invoice', 0)->orWhere('total_mileage', 0);
dd($deliveries);

此查询有效:

$deliveries = $this->instance->delivery
        //->where('amount_invoice', 0);
        ->where('total_mileage', 0);
        //->where('amount_invoice', 0)->where('total_mileage', 0);
        //->where('amount_invoice', 0)->orWhere('total_mileage', 0);
dd($deliveries);

此查询有效:

$deliveries = $this->instance->delivery
        //->where('amount_invoice', 0);
        //->where('total_mileage', 0);
        ->where('amount_invoice', 0)->where('total_mileage', 0);
        //->where('amount_invoice', 0)->orWhere('total_mileage', 0);
dd($deliveries);

此查询无效:

$deliveries = $this->instance->delivery
        //->where('amount_invoice', 0);
        //->where('total_mileage', 0);
        //->where('amount_invoice', 0)->where('total_mileage', 0);
        ->where('amount_invoice', 0)->orWhere('total_mileage', 0);
dd($deliveries);

我不知道问题是什么,但使用orWhere失败

1 个答案:

答案 0 :(得分:2)

在不知道你的模特关系是什么样的情况下,我猜错了

$this->instance->delivery

给你一个回收集,因为这是Laravel通常做的事情。 ->where(...)用于过滤Laravel中的集合,但->orWhere(...)没有。

如果你有

$this->instance->delivery()

相反,您将获得一个查询构建器,然后您可以使用该构建器执行->where(...)->orWhere(...)查询。不要忘记在最后添加->get()电话。