Virgil Cruz
Virgil Cruz

Reputation: 191

Laravel: How to insert multiple records

I want to insert multiple records in my database using Laravel, however when i insert it, it only gives me one record in the database

Here's my Controller

    public function postCreateAttendance()
{
    $validate = Validator::make(Input::all(), array(
        'status' => 'required'
    ));


    if ($validate->fails())
    {
        return Redirect::route('viewStudent')->withErrors($validate)->withInput();
    }
    else
    {





        foreach(User::all() as $user):
        foreach(User::whereRaw('isTeacher = "0" and isAdmin = "0"')->get() as $student)
        foreach(User::whereRaw('isTeacher = "1" and isAdmin = "0"')->get() as $teacher)
        //$users[$user->id]=$user->firstname;




        $attendance = new Attendance();
        $attendance->status = Input::get('status');
        $attendance->comment = Input::get('comment');
        $attendance->student_id = $student->id=$student->id;
        $attendance->student_firstname = $student->id=$student->firstname;
        $attendance->student_lastname = $student->id=$student->lastname;
        $attendance->teacher_id = $teacher->id=$teacher->id;
        $attendance->teacher_firstname = $teacher->id=$teacher->firstname;
        $attendance->teacher_lastname = $teacher->id=$teacher->lastname;






        if($attendance->save())
        {
            return Redirect::route('viewStudent')->with('success', 'ATTENDANCE HAS BEEN RECORDED!');
        }
    else
    {
        return Redirect::route('viewStudent')->with('fail', 'An error occured while creating the attendance.');
    }


        endforeach;


    }
}

How can i save multiple records? Please help Thank You ^_^

Upvotes: 1

Views: 1666

Answers (1)

Laurence
Laurence

Reputation: 60068

The issue is that you are returning during the foreach loop - so only one record is processed. You need to process all the records, then return the route.

Here is some refactoring of your code

public function postCreateAttendance()
{
    $validate = Validator::make(Input::all(), array(
        'status' => 'required'
    ));


    if ($validate->fails()) {
        return Redirect::route('viewStudent')->withErrors($validate)->withInput();
    }

        foreach(User::where('isTeacher', '0')->where('isAdmin', '0')->get() as $student) {
              foreach(User::where('isTeacher', '1')->where('isAdmin', '0')->get() as $teacher) {

                    $attendance = new Attendance();
                    $attendance->status = Input::get('status');
                    $attendance->comment = Input::get('comment');
                    $attendance->student_id = $student->id;
                    $attendance->student_firstname = $student->firstname;
                    $attendance->student_lastname = $student->lastname;
                    $attendance->teacher_id = $teacher->id;
                    $attendance->teacher_firstname = $teacher->firstname;
                    $attendance->teacher_lastname = $teacher->lastname;
                    $attendance->save();
               }
          }

      return Redirect::route('viewStudent')->with('success', 'ATTENDANCE HAS BEEN RECORDED!');
}

Edit: I've removed the first foreach(User::all() as $user): - because at the moment, in your code, it does nothing...?

Upvotes: 1

Related Questions