Hi @jvbalcita try
ScheduleTime::on($db)->createMany($schedules);
ScheduleDay::on($db)->createMany($days);
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I'm having a problem trying to insert an array with multiple object.
I have 3 tables Schedules, ScheduleTime, ScheduleDays
The Schedule Time and Days should be multiple insert. It works fine when we tried to insert using a single object. But having a problem inserting multiple data when the object is not inside the array.
The ScheduleDays is also an array.
I tried something like bellow. Would someone please help to correct the controller method -
Here's the Schedule controller.
$schedule = new Schedule;
$schedule->setConnection($db);
$schedule->section_id = $request->section_id;
$schedule->save();
$schedules = [];
if (isset($request->time_start) && count($request->time_start) > 0) {
foreach ($request->time_start as $day => $v) {
$schedules[] = [
'schedule_id' => $schedule->id,
'subject_id' => $request->subject_id,
'time_start' => $request->time_start[$day],
'time_end' => $request->time_end[$day],
'duration' => $request->duration[$day],
'created_at'=>$now,
'updated_at'=>$now
];
}
}
ScheduleTime::on($db)->insert($schedules);
$days = [];
if (isset($request->days) && count($request->days) > 0) {
foreach ($request->days as $day => $v) {
// $daysArr = $request->input('days');
// $daysData = implode(",", $daysArr);
$days[] = [
'schedule_id' => $schedule->id,
'days' => $request->days[$day],
'created_at'=>$now,
'updated_at'=>$now
];
}
}
ScheduleDay::on($db)->insert($days);
And this is what it looks like
Array
(
[0]:
day: (2) ["Mon","Tues"]
duration: [60]
section_id: 1
subject_id: 1
time_start: ["8:00"]
time_end: ["9:00"]
[1]:
day: (2) ["Mon","Tues"]
duration: [60]
section_id: 1
subject_id: 1
time_start: ["8:00"]
time_end: ["9:00"]
[2]:
day: (2) ["Mon","Tues"]
duration: [60]
section_id: 1
subject_id: 1
time_start: ["8:00"]
time_end: ["9:00"]
)
Please or to participate in this conversation.