vendredi 18 mai 2018

Error while inserting multiple records in Laravel5

Have a number of select menus that are to be inserted into a table in bulk.


    <select selected="" class="form-control course" name="course[]">
        <option disabled selected value> -- select an option -- </option>
        @foreach($course as $key)
            <option></option>
        @endforeach
    </select>

    <select selected="" class="form-control course" name="course[]">
        <option disabled selected value> -- select an option -- </option>
        @foreach($course as $key)
            <option></option>
        @endforeach
    </select>

Adding the values from course[] to an array in the controller.

    for($i = 0; $i < 2; $i++) {
        $data = array(
            array('Package_Id' => 1, 'Course_Id' => $request->course[$i],'University_Id' => 42, 'Package_title' => 'Test insert', 'Course_name' => 'Test insert'),
            array('Package_Id' => 2, 'Course_Id' => $request->course[$i],'University_Id' => 11, 'Package_title' => 'Test insert2', 'Course_name' => 'Test insert2')
        );
    }

Course_package::insert($data);

But keep getting the error:

SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: 'l' for column 'Course_Id' at row 1 (SQL: insert into `course_package` (`Course_Id`, `Course_name`, `Package_Id`, `Package_title`, `University_Id`) values (l, Test insert, 1, Test insert, 42), (l, Test insert2, 2, Test insert2, 11))

It works when running:

    $data = array(
        array('Package_Id' => 1, 'Course_Id' => $request->course[0],'University_Id' => 42, 'Package_title' => 'Test insert', 'Course_name' => 'Test insert')
    );

But then only one record is available for insert.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire