У меня есть таблица, где она имеет сумму, Director_id.Я пытаюсь создать массив, добавив количество того же идентификатора директора, чтобы я мог передать его на blade-сервер для создания таблицы.но он игнорирует последние значения для каждого идентификатора.
Ниже приведен мой код, но он выдает неверный результат
$expense_details = DB::table('receipts')
->join('directors', 'directors.id', '=', 'receipts.director_id')
->join('account_type', 'account_type.id', '=', 'receipts.type')
->join('expense_type', 'expense_type.id', '=', 'receipts.exp_earn_type')
->select('receipts.*', 'directors.name as directors_name','account_type.name as account_name', 'expense_type.name as exp_name')
->get();
dd($expense_details);
$investor_total_by_id=[];
$exp_amount=0;
foreach ($expense_details as $exp){
if (isset($investor_total_by_id[$exp->director_id])) {
$exp_amount= $exp_amount + $exp->amount;
$investor_total_by_id[$exp->director_id] = $exp_amount;
}else {
$investor_total_by_id[$exp->director_id] = $exp->amount;
}
}
//dd($investor_total_by_id);
return view('Accounts/Investment/add')
->with('expense_details',$expense_details)
->with(' investor_total_by_id', $investor_total_by_id)
;
Массив таблицы такой, как я пытаюсь сгруппировать / добавить суммудля каждого Director_id
Основная таблица
Collection {#522 ▼
#items: array:6 [▼
0 => {#526 ▼
+"id": 24
+"type": 2
+"exp_earn_type": 11
+"sale_id": 0
+"receipt_date": "2019-04-29 00:00:00"
+"amount": 100.0
+"description": "aesf"
+"device_imei": "0"
+"operator_id": 2
+"director_id": 1
+"emp_id": 0
+"created_at": "2019-04-18 07:26:10"
+"updated_at": "2019-04-18 07:26:10"
+"directors_name": "Siam"
+"account_name": "Expenses"
+"exp_name": "Travel Expense"
}
1 => {#530 ▼
+"id": 25
+"type": 2
+"exp_earn_type": 9
+"sale_id": 0
+"receipt_date": "2019-04-16 00:00:00"
+"amount": 1000.0
+"description": "ff"
+"device_imei": "0"
+"operator_id": 2
+"director_id": 1
+"emp_id": 0
+"created_at": "2019-04-18 15:58:11"
+"updated_at": "2019-04-18 15:58:11"
+"directors_name": "Siam"
+"account_name": "Expenses"
+"exp_name": "GpsTrackingDevices"
}
2 => {#528 ▼
+"id": 26
+"type": 2
+"exp_earn_type": 18
+"sale_id": 0
+"receipt_date": "2019-04-17 00:00:00"
+"amount": 1000.0
+"description": "ttt"
+"device_imei": "0"
+"operator_id": 2
+"director_id": 1
+"emp_id": 0
+"created_at": "2019-04-18 17:45:54"
+"updated_at": "2019-04-18 17:45:54"
+"directors_name": "Siam"
+"account_name": "Expenses"
+"exp_name": "Computer – Hardware"
}
3 => {#527 ▼
+"id": 27
+"type": 2
+"exp_earn_type": 13
+"sale_id": 0
+"receipt_date": "2019-04-15 00:00:00"
+"amount": 5000.0
+"description": "gg"
+"device_imei": "0"
+"operator_id": 2
+"director_id": 1
+"emp_id": 3
+"created_at": "2019-04-18 21:01:37"
+"updated_at": "2019-04-18 17:46:59"
+"directors_name": "Siam"
+"account_name": "Expenses"
+"exp_name": "Payroll – Salary & Wages"
}
4 => {#525 ▼
+"id": 28
+"type": 2
+"exp_earn_type": 17
+"sale_id": 0
+"receipt_date": "2019-04-15 00:00:00"
+"amount": 222.0
+"description": "ddd"
+"device_imei": "0"
+"operator_id": 2
+"director_id": 2
+"emp_id": 0
+"created_at": "2019-04-18 19:22:36"
+"updated_at": "2019-04-18 19:22:36"
+"directors_name": "Riad"
+"account_name": "Expenses"
+"exp_name": "Rent Expense"
}
5 => {#529 ▼
+"id": 29
+"type": 2
+"exp_earn_type": 16
+"sale_id": 0
+"receipt_date": "2019-04-11 00:00:00"
+"amount": 222.0
+"description": "22"
+"device_imei": "0"
+"operator_id": 2
+"director_id": 2
+"emp_id": 0
+"created_at": "2019-04-18 19:22:54"
+"updated_at": "2019-04-18 19:22:54"
+"directors_name": "Riad"
+"account_name": "Expenses"
+"exp_name": "Office Supplies"
}
]
}
Я получаю этот результат
array:2 [▼
1 => 7000.0
2 => 7222.0
]
, но он должен быть
array:2 [▼
1 => 7100.0
2 => 444.0
]