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

coder72's avatar

How to sum two table (price and dicount) if date match though item

This is my item database look like

| id | name | description | created_at |
| 1  | Test | ............| 2021/1/29

This is my sale table.My price and discount store in serialized array

id |item_id| title |          price          |       discount     |       created_at
1  |   1   |test1 | ["3200","2300","4500"]  |["500","200","500"] | 2021/1/29
2  |   1   |test2 | ["2300","6200","8500"]  |["200","300","400"] |2021/1/30

This is my trade table

id | item_id|title  | price | discount | created_at
1  |      1   |test1 | 5230  |6500 | 2021/1/29
2  |      1  |test2 | 9000  |7800 |2021/1/30

This is my Item Model.I need to add all price and discount both of sale and trade of related item

public function sale(){
 	return $this->hasMany(Sale::class, 'item_id', 'id');
}
public function trade(){
 	return $this->hasMany(Trade::class, 'item_id', 'id');
}
public function sum_total(){
	
}
array:3 [▼
  0 => {#1663 ▼
    +"created_at": "2021/1/29"
    +"price":15230
    +"discount": 7700
  }
  1 => {#1702 ▼
    +"created_at": "2021-01-30 "
    +"price": 26000
    +"discount": 8700
  }
]
0 likes
2 replies
ep!sode's avatar

Why would you sum up price and discount though?

Please or to participate in this conversation.