Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AbdulBazith's avatar

How to make total of products based on product category laravel

Guys iam working with a project Inventory Control like project.

I have a table product_category with columns

id(pk),
login_user_id,
product_category_name

ans this is my product table with columns

id(pk),
login_user_id,
product_name,
product_category_id,

and this is purchase_product table with columns

id(pk),
login_user_id,
date,
product_id,
qty,
total_amount


Now what my client expecting is i need to have the total amount based on the category of the product.

say for example.

if i have data in purchase_product table like


date        Product             qty     total_amount    
30-03       coconut oil         10      1000
30-03       ground nut oil      20      2000
30-03       Seasame oil         30      3000
30-03       Onion               10      200
30-03       tomato              20      200

in the above table first three belongs to oil type category. and second two belongs to vegetables category.

now how i need the total is

Oil - 6000

Vegetables -- 400

i need to get the total amount of the products based in its category.

here whats the problem is i didnt give product_category in the purchase_product table

but i have product_category table and product table with relationship.

How can i do this please Kindly help me for this.

i need to fetch based on date.

i have a date field in my blade file. i will choose the date and click the search button.

it moves to search_purchase_product method in controller there i need to search this date and the total amount with based on product category.

How to do this please Kindly help mee please.

I know the i need to use groupby

but whats my problem is in my purchase_product table i dont have product_category column

so what should i don please some one help.

and these are my models

ProductCategory

Product

PuchaseProduct

Kindly some one help please...

0 likes
5 replies
Snapey's avatar

I would use joins first and then groupBy

AbdulBazith's avatar

@snapey thank you soo much for you response. thank you.

please please slightly explain please.

actually i asked this question on Saturday, no one replied. so only i tagged you sorry.

Please slighty explain. do i need to add the product_category column to purchase_product table??

else is there any solution please

Snapey's avatar
Snapey
Best Answer
Level 122

No, sorry, got to head to work in a minute

$purchases = PurchaseProduct::join('product', 'product.id', '=', 'purchase_product.product_id' )
        ->join('product_category', 'product_category.id', '=', 'product.product_category_id')
        ->select('purchase.product.id','product_name','qty','product_category.id' as 'category_id')
        ->get()

Sorry, got to dash

Start with this, add whatever columns you need from the three tables, add sql aggregate once you know you are getting all the joined data ok

AbdulBazith's avatar

@snapey thank you soo much for your response.. thank you for responding fast for my need.

what i did is added the product_category field in purchase_product table... so i got the total value based on the group..

now i am facing another big problem, with the same relation problem

Kindly if possible please answer for that please.

link: https://laracasts.com/discuss/channels/laravel/how-to-get-total-based-on-income-type-group-with-different-id

Please or to participate in this conversation.