Wolrab Wolrab - 1 year ago 91
MySQL Question

Laravel Where Not Equal To x 2

I have a model which I'm trying to query using 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?

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 i.e. delivered and invoiced.

Answer Source

What about this:

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


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