0

I have below table consols:

Schema::create('consols', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->double('current_weight', 8, 3)->default(0);
    $table->double('current_weight', 8, 3)->default(0);
    $table->double('final_weight', 8, 3)->default(0);
    $table->double('final_cbm', 8, 3)->default(0);    
    $table->timestamp('finals_sent', 0)->nullable();
});

Which contain an entry like below:

id | current_weight | current_cbm | final_weight | final_cbm | finals_sent         |
-----------------------------------------------------------------------------------|
1  | 45.000         | 1.000       | 200.000      | 10.000    | 2019-09-26 10:03:59 |

I have written a scope on my model Consol, to filter the entries, where: finals_sent is not null, current_weight > final_weight OR current_cbm > final_cbm:

public function scopeOfCurrentGreaterThanFinals($query)
{
    return $query->whereNotNull('finals_sent')->where(function ($query) {
        $query->where('current_weight', '>', 'final_weight')
            ->orWhere('current_cbm', '>', 'final_cbm');
    });

}

When I use it and dump it out - I am expecting to see no results:

$consols = Consol::ofCurrentGreaterThanFinals()
           ->orderBy('awb', 'DESC')
           ->get()
           ->toArray();

However, the entry above is still being shown:

array:1 [▼
  0 => array:24 [▼
    "id" => 1
    "current_weight" => 45.0
    "current_cbm" => 1.0
    "final_weight" => 200.0
    "final_cbm" => 10.0
    "finals_sent" => "2019-09-26 10:03:59"
  ]
]

I am not sure what I am doing wrong? I think I cleary states that I only want to see the results, where:

  • current_weight > final_weight or
  • current_cbm > current_weight and
  • finals_sent is not null

And above entry does not fit this, because the current_weight and current_cbm is clearly not higher than the final_weight and final_cbm.

What am I doing wrong?

2
  • what about your offWeek scope the Finals scope seems fine Commented Sep 26, 2019 at 8:43
  • Removed that from the question. Commented Sep 26, 2019 at 8:46

1 Answer 1

1

Ah ok, here is the issue. In order to compare the two columns you have to use whereRaw() as where for the second parameter expects a value, not a column. So this should work, I tested with your database and values.

return $query->whereNotNull('finals_sent')->where(function ($q) {
            $q->whereRaw('current_weight > final_weight')
                ->orWhereRaw('current_cbm > final_cbm');
        });
Sign up to request clarification or add additional context in comments.

2 Comments

I just updated the question. I actually did intend to get the results if current_weight or current_cbm is higher than their final_ counterparts. However, it still shouldn't return any results.
@oliverbj Updated my answer. Hope this works now. Sorry for the small misunderstanding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.