skip to Main Content

Not equal to function is not working in below code when I am running it through Postman, but the same query is giving output in MySQL.
Laravel Eloquent Code –

Invoice::where('invoices.centre_code', $userInfo->centre_code)
                    ->where("invoices.itemTotalPrice","<>", "invoices.paymentCollected") // this line is not working
                    ->join('customers', 'customers.customer_code', '=', 'invoices.customerCode')
                    ->join('items', 'items.id', '=', 'invoices.itemId')
                    ->select('invoices.*', 'customers.customerName', 'customers.customerDialCode', 'customers.customerCity', 'customers.customerAddress', 'customers.customerMobile', 'items.itemName')
                    ->whereBetween('invoices.created_at', [$from, $to])
                    ->oldest("invoices.nextPaymentDate")
                    ->get();

Above code returning this SQL query, and when I ran it MySQL cmd it working perfectly.

select `invoices`.*, `customers`.`customerName`, `customers`.`customerDialCode`, `customers`.`customerCity`, `customers`.`customerAddress`, `customers`.`customerMobile`, `items`.`itemName` from `invoices` inner join `customers` on `customers`.`customer_code` = `invoices`.`customerCode` inner join `items` on `items`.`id` = `invoices`.`itemId` where `invoices`.`centre_code` = 705405 and `invoices`.`itemTotalPrice` <> invoices.paymentCollected and `invoices`.`created_at` between 2023-03-24 00:00:00 and 2023-03-24 23:59:59 order by `invoices`.`nextPaymentDate` asc

Exactly this code is not working ->where("invoices.itemTotalPrice","<>", "invoices.paymentCollected")

We are expecting the data where invoices.itemTotalPrice != invoices.paymentCollected, but whenever we ran the code it fetching all data within the given date. If invoices.itemTotalPrice == invoices.paymentCollected it should exclude those rows, but this is not working. We are using Laravel 10.0

3

Answers


  1. Seeing the log you had pasted in a comment, we can see <> being treated as =

    To simplify the problem you could use:

    ->whereNot("invoices.itemTotalPrice", "invoices.paymentCollected") 
    
    Login or Signup to reply.
  2. The where eloquent helper accepts either an array of a series of arguments.

    The series of arguments can be either

    1. Column | value (this will equate to column=value
    2. Column | operator | value (this will equate to column<operator>value

    So in your given eloquent code your invoices.paymentCollected is being evaluated as a value

    If you need to place constraints on joins please use the syntax described here

    Or you can use self joins if the columns are part of the same table

    Or you can use the whereColumn syntax as mentioned by @Pippo

    Login or Signup to reply.
  3. Use ->whereColumn ('invoices.itemTotalPrice', '<>', 'invoices.paymentCollected') to compare columns

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search