Eloquent: 3 Ways to Sum Pivot Column in Many-to-Many Relationship

Eloquent: 3 Ways to Sum Pivot Column in Many-to-Many Relationship

Laravel Daily

3 года назад

17,086 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Jacques Oscar Ndjebayi Matike
Jacques Oscar Ndjebayi Matike - 31.08.2023 13:25

Saved me ! Thanks

Ответить
j5
j5 - 08.02.2023 02:25

Thanks

Ответить
Antonio Cabral
Antonio Cabral - 09.06.2022 01:54

Thanks a million!

Ответить
noelkalel
noelkalel - 19.12.2021 00:47

hi povilas, was wondering, is it possible to get sum of quantity (order_product pivot table) * price (products table) for desired order_id (order_product pivot table)

Ответить
Lasse Maerkedahl Larsen
Lasse Maerkedahl Larsen - 16.12.2021 21:29

How would you calculate a sum of two columns multiplied? Like price and amount ( qty )

Ответить
Víctor González Toledo
Víctor González Toledo - 16.09.2021 18:03

Thanks as always!

Ответить
CodeGorilla 04
CodeGorilla 04 - 03.09.2021 15:37

Thnx so much, I was stuck on this for a bit

Ответить
Asatbek Xalimjonov
Asatbek Xalimjonov - 28.07.2021 15:38

Amazing

Ответить
Pardis Zaryab
Pardis Zaryab - 07.05.2021 23:06

Hello sir, Thank you very much for your great tutorials, I have a problem with displaying mysql column value in my page for example I have a profile table each user has different title such as ( Manager, Hr, Finance, teachers, and parents) I know how to count all table and know how many users are registered in my DB but I want to count each title in a separate line like ( total number of teachers, total number of parents or etc.) please help me how to do it. Thank you very much and appreciate your help in advance. V/r, Pardis

Ответить
Sam Smekens
Sam Smekens - 06.05.2021 21:12

Is there a way to do the withSum but with a calculation on two columns?
For example if you have an order table and a product_lines table. The product_line table contains price and quantity. How would you calculate the total amount of the order if order total amount = sum of (product_line_price * product_line_quantity)?

Ответить
renwar
renwar - 06.05.2021 14:10

WithSum('products as totalItems', '.....') to change the name.

Ответить
edrees abobker
edrees abobker - 05.05.2021 03:24

Hi Mr. Pavilas, I have a question about the best way to store the product price which has more than size or may be one size, for example pizza or burgers

Ответить
Mourad Ouddah
Mourad Ouddah - 04.05.2021 19:05

Goooood

Ответить
prabin Khadka
prabin Khadka - 04.05.2021 18:16

will this work on laravel 7 ? withSum() ? I need total in datatable . For now i'm doing by foreach loop and was searching for efficient way

Ответить
Daniel Sepp
Daniel Sepp - 04.05.2021 16:37

This design is flawed by design. In a POS you would usually have Order->OrderRow that would have a relation to Product or anything else. This is because sometimes you need to discount a row of an Order, not a specific product.

Ответить
Waqas Marri
Waqas Marri - 04.05.2021 15:05

Thanks for responding to email. Much appreciated

Ответить
MallesBixie
MallesBixie - 04.05.2021 14:22

It's a shame we didn't get to see the resulting query that Eloquent produced for the counts. Great tip!

Ответить
Hugo Ducommun
Hugo Ducommun - 04.05.2021 13:26

Thank You Povilas, I just needed this feature for my work !

Ответить