0

I am not sure if this is possible to do without doing a query first.

I have something like this:

$item_weight

This item goes into a box, which has its own row $box->weight and has a max weight $box->max_weight.

I want a list of boxes that I could put my item into.

I know I can do this by doing a query first and then calculating with a foreach but I wonder if there is a cleaner way to do this.

Something like this:

$boxes= Boxes::where('max_weight','>',$item_weight + $box->weight)
                ->orderBy('max_weight','asc')
                ->get();

1 Answer 1

2

You can use whereRaw and shift calculation to db side:

$boxes= Boxes::whereRaw('boxes.max_weight - boxes.weight > ?', [$item_weight])
    ->orderBy('max_weight','asc')
    ->get();
Sign up to request clarification or add additional context in comments.

Comments

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.