0

I have a detail table where there is a relationship with the product table, in the detail table I have a product_id field.

I try to multiply the quantity that appears in the detail table and the price that appears in the product table, used DB ::raw but I get a column error not found.

QueryScope in Detalle Model

 $query->with(['product' => function ($query) {
            $query->select('id', 'name', 'price');
        }])
        ->select('*', DB::raw('SUM(count*products.price) as fullcount'))
        ->groupBy('product_id');
4
  • what table/alias count column belongs to? Commented Nov 12, 2018 at 19:46
  • @Alex count in table details Commented Nov 13, 2018 at 18:28
  • I don't see any details table involved. Commented Nov 13, 2018 at 19:17
  • @Alex That queryScope is inside the model Details Commented Nov 13, 2018 at 20:13

1 Answer 1

1

Use a JOIN:

$query->select('product_id', DB::raw('SUM(count*products.price) as fullcount'))
    ->join('products', 'details.product_id', '=', 'products.id')
    ->groupBy('product_id');
Sign up to request clarification or add additional context in comments.

2 Comments

An additional query, what I want is that you group them if a field of the detail table has a certain value, otherwise you leave them separated Is it possible?
I don't think so.

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.