Laravel Livewire Dynamic Form Data Not Working

Advertisements

Quite new to Laravel Livewire. I’m trying to create a dynamic form for application but I couldn’t quite understand how to attach additional data when storing.
The user selects the instructor(faculty_id), schoolyear(sy) and semester(sem). And new schedule and option to add more rows()

This is from my controller store() method

 public function store()
{
    
    $order = Emp_sched::create([
        'faculty_id'=>$this->faculty_id,
        'sem'=>$this->sem,
        'sy'=>$this->sy,
    ]);

    foreach ($this->createScheds as $sched) {
        $order=(['corsdes' => $sched['corsdes']],
        ['c_time' => $sched['c_time']],  ['day' => $sched['day']],  ['room' => $sched['room']]);
    }


    return 'Schedules Saved!';
}

>Solution :

You must call Model::create inside loop

public function store()
{
    
    foreach ($this->createScheds as $sched) {
        $createArray = array_merge([
                'faculty_id' => $this->faculty_id,
                'sem' => $this->sem,
                'sy' => $this->sy,
            ],[
                'corsdes' => $sched['corsdes'],
                'c_time' => $sched['c_time'],
                'day' => $sched['day'],
                'room' => $sched['room'],
            ]);
            Emp_sched::create($createArray);
    }


    return 'Schedules Saved!';
}

Leave a ReplyCancel reply