skip to Main Content

I have a weird eloquent bug.

In my customer_order_lines table I have two columns qty_reserve and qty_in_pick_up.
Both are integer

For example, in the database I have one line

product_id qty_reserve qty_in_pick_up
1 4 4

In eloquent I try to load the line that qty_reserve are not equal to qty_in_pick_up

$lines = CustomerOrderLine::where('qty_reserve','!=','qty_in_pick_up')->get();

But for an unknow reason this load the line where both qty_reserve and qty_in_pick_up are equal to 4.

But if I do the same but with whereRaw it’s working

$lines = CustomerOrderLine::whereRaw('qty_in_pick_up != qty_reserve')->get();

Anyone has an idea why.

2

Answers


  1. Chosen as BEST ANSWER

    After posting my question, I add a flash on the where clause. I check the Laravel documentation and where expect the second argument/third to be a value not and other columns

    I tried the whereColumn and it solves my problem. It's cleaner than the whereRaw and expect a column as a second/third argument.

    I leave the answer here if that help someone else.


  2. $lines = CustomerOrderLine::where('qty_reserve','!=','qty_in_pick_up')->get();
    

    This can’t work, because where() waiting for the value for the third parameter.

    You can use whereColumn('qty_reserve', '!=', 'qty_in_pick_up');

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