Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jackFlick's avatar

How to insert multiple single array with multiple object

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"]
)
0 likes
4 replies
jackFlick's avatar

HI @silencebringer, this one generates "Call to undefined method Illuminate\\Database\\Eloquent\\Builder::createMany()", The insert actually works if multiple object is passed without the Days array. but with days array for example I passed Monday, Tuesday and Wednesday for Array 0 and Monday Tuesday for Array 1 it will insert only Wednesday from [0] and Monday from [1].

This is what we need as well to achieve in days array.

days: ([
	[0] => ["Monday", "Tuesday", "Wednesday"]
	[1] => ["Monday", "Tuesday"]
])
Snapey's avatar

what type of column is days where you expect to store an array in it ?

jackFlick's avatar

It's a string on a new table. But got it working now by converting the data into json and instead of inserting multiple days with ID. Now it will only insert as one id but in json format ["Monday", "Tuesday", "Wednesday"].

Is that an okay approach? or is there a better one?

Please or to participate in this conversation.