I want to show current week's data. In databse I have some perticular date data I have fill the missing dates and in view page I am showing that by foreach loop because I want to set different values for them. But it is showing mutliple times data because of 2 foreach loop. I want to get result in one array. My code is:
public function cashflowdetails() {
$cashflow = CashModel::orderBy('payment_date','asc') ->get();
$cashflow_array = [];
$cashflowDate_array = [];
$now = Carbon::now();
if (Input::get('from') != '') {
$weekStartDate =Input::get('from') ;
}
else{
$weekStartDate = Carbon::now()->startOfWeek();
}
if (Input::get('to') != '') {
$weekEndDate = Input::get('to');
}
else{
$weekEndDate = Carbon::now()->endOfWeek()->addDay();
}
$period = CarbonPeriod::create($weekStartDate, $weekEndDate);
foreach ($period as $date) {
$dt = new DateTime($date->format("Y-m-d"));
if($dt->format('l')!= 'Sunday' && $dt->format('l')!= 'SUNDAY' && $dt->format('l')!= 'SATURDAY' && $dt->format('l')!= 'Saturday')
{
foreach($cashflow as $cash){
if($date->format("Y-m-d") === $cash->payment_date )
{
$dbData['id'] = $cash->id;
$dbData['payment_date'] = $cash->payment_date;
$d = new DateTime($cash->payment_date);
$dbData['day'] = $d->format('l');
$dbData['opening_balance'] = $cash->opening_balance;
$dbData['other_income'] = $cash->other_income;
$dbData['rent'] = $cash->rent;
$dbData['labour'] = $cash->labour;
$dbData['electricity'] = $cash->electricity;
$dbData['creditors'] = $cash->creditors;
$dbData['insurance'] = $cash->insurance;
$dbData['direct_debits'] = $cash->direct_debits;
$dbData['others'] = $cash->others;
$dbData['total_amount'] = $cash->total_amount;
$dbData['updated_at'] = $cash->updated_at;
$dbData['created_at'] = $cash->created_at;
print_r($cash->payment_date);
$cashflow_array[] = $dbData;
}
else{
$dbData['id'] = null;
$dbData['payment_date'] = $date->format("Y-m-d");
$d = new DateTime($date->format("Y-m-d"));
$dbData['day'] = $d->format('l');
$dbData['labour'] = 0;
$dbData['opening_balance'] = 0;
$dbData['other_income'] = 0;
$dbData['rent'] =0;
$dbData['electricity'] = 0;
$dbData['creditors'] = 0;
$dbData['insurance'] =0;
$dbData['direct_debits'] = 0;
$dbData['others'] = 0;
$dbData['total_amount'] = 0;
$dbData['updated_at'] = '';
$dbData['created_at'] = '';
$cashflowDate_array[] = $dbData;
}
//break;
}
}
}
$cashflow = array_replace($cashflowDate_array, $cashflow_array);
return $cashflow;
}
