简体   繁体   English

Laravel 不等于 x 2

[英]Laravel Where Not Equal To x 2

I have a model which I'm trying to query using eloquent.我有一个模型,我正在尝试使用 eloquent 进行查询。

In my table I have the following columns.在我的表中,我有以下列。

Order ID
Invoiced -> default(0)
Delivered -> default(0)

I want to query this table and only show orders where not invoiced and not delivered but can show orders where are delivered but not invoiced or are invoiced but not delivered.我想查询此表,只显示未开票但未交付的订单,但可以显示已交付但未开票或已开票但未交付的订单。

How would I do this in Eloquent?我将如何在 Eloquent 中做到这一点?

Here's my code:这是我的代码:

$orders = Order::where('delivered', '<>', 1)->where('invoiced', '<>', 1)->get();

The above works however when I update an order to delivered but the invoice is still outstanding this removes the order from the list.但是,当我将订单更新为已交付但发票仍未结清时,上述方法有效,这会从列表中删除该订单。

I want to exclude all orders where the are complete ie delivered and invoiced.我想排除所有完整的订单,即已交付并开具发票。

What about this:这个怎么样:

$orders = Order::where('delivered', '<>', 1)->where('invoiced', '<>', 1)->get();

Update更新

Did not test, but try this for 'should show where orders are delivered not invoiced, or invoiced but not delivered':没有测试,但试试这个“应该显示订单交付的地方没有开票,或开票但没有交付”:

$orders = Order::where(function ($query) {
            $query->where('delivered', 1)->where('invoiced', '<>', 1);
        })->orWhere(function ($query) {
            $query->where('delivered', '<>', 1)->where('invoiced', 1);
        })->get();

Something like this might solve your problem像这样的事情可能会解决您的问题

.. whereNotIn( 'id', [$user_id])->get();

in this solution, you can choose a range of id s or one id.在此解决方案中,您可以选择一系列 id 或一个 id。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM