I have a problem trying to save arrays in a table in my database.
I have the code in this way in the store()
function of my controller:
try {
foreach($request->horario as $id_horario=>$row){
foreach($row['check'] as $check){
$startdate = $row['desde'];
$endate = $row['hasta'];
$period = CarbonPeriod::create($startdate, $endate);
$period_2 = CarbonPeriod::create($startdate, $endate);
$dates = $times = [];
$dates_2 = $times_2 = [];
$tstart = Carbon::createFromFormat('H:i:s', $row['hora_inicio']);
$tend = Carbon::createFromFormat('H:i:s', $row['hora_termino']);
$tstart_2 = Carbon::createFromFormat('H:i:s', $row['hora_inicio']);
$tstart_2->modify('+1 hour');
$tend_2 = Carbon::createFromFormat('H:i:s', $row['hora_termino']);
while ($tstart < $tend){
$times[] = $tstart->format("H:i:s");
$tstart->addHour(); //
}
while ($tstart_2 <= $tend_2) {
$times_2[] = $tstart_2->format('H:i:s');
$tstart_2->addHour();
}
foreach($period as $date){
foreach($times as $time){
$dates[] = $date->format("Y-m-d") . " " . $time;
}
}
foreach ($period_2 as $date_2) {
foreach ($times_2 as $time_2) {
$dates_2[] = $date_2->format('Y-m-d'). " ".$time_2;
}
}
$cuenta = count($dates);
for($i = 0; $i < $cuenta; $i++){
$horarios[]= [
'hora_incio' => $dates[$i],
'hora_fin' => $dates_2[$i],
'estado_horario_id' => $row['estado_horario_id'],
'cancha_id ' => $check
];
}
/*ESTA PARTE ME ARROJA EL ERROR*/
HorariosNew::create(['hora_incio' => $dates[$i],'hora_fin' => $dates_2[$i],'estado_horario_id' => $row['estado_horario_id'], 'cancha_id ' => $check]);
$horarios->save();
}
}
}
And the error thrown at me is Undefined offset: 2,
I'm not sure why I get that error.