Tabelle: Transaktionstypen
Code: Select all
| id | type_hash | description     | category |
|----|-----------|-----------------|----------|
| 1  | abcd      | sale price      | sale     |
| 2  | dbac      | sale tax        | sale     |
| 3  | agft      | sale shipping   | sale     |
| 4  | pgsk      | refund price    | refund   |
| 5  | sa2r      | refund tax      | refund   |
| 6  | sdf4      | refund shipping | refund   |
Code: Select all
| id | type_hash | amount |
|----|-----------|--------|
| 1  | abcd      | 12     |
| 2  | dbac      | 14     |
| 3  | agft      | 19     |
| 4  | pgsk      | -20    |
| 5  | sa2r      | -12    |
| 6  | sdf4      | -7     |
Code: Select all
public function transactionType() : BelongsTo
{
return $this->belongsTo(TransactionType::class, 'type_hash', 'type_hash');
}
- Betrag Aggregierte Summe (Betrag) als Betrag
- Transaktionen nach TransactionType.category gruppieren
Code: Select all
| Results | transactionType.category | sum(amount)   |
|---------|--------------------------|---------------|
| 1       | sale                     | 45            |
| 2       | refund                   | -39           |
Code: Select all
Transaction::selectRaw('sum(amount) as amount')
->with('transactionType')
->get()
->groupBy('transactionType.category');
Code: Select all
Transaction::selectRaw('sum(amount) as amount')
->with(['transactionType' => function($query){
$query->select('category')->groupBy('category');
}])
->get();
 Mobile version
 Mobile version