Problem with laravel mass assigment . Works with "ModelName::insert($array)" but not with "ModelName::create($array)"

56 Views Asked by At

In the sample code below I want to get all saved students ID each time using assignment:


for ($i=0; $i < $count; $i++)
        { 
         $student_array[] = array(
                      'user_id'                  =>   $login_user_id,
                      'first_name'               =>   $student_first_name[$i],
                      'middle_name'              =>   $student_middle_name[$i],
                      'last_name'                =>   $student_last_name[$i],
                      'siblings_first_name'      =>   $siblings_first_name ?? NULL,
                      'siblings_middle_name'     =>   $siblings_middle_name ?? NULL,
                      'siblings_last_name'       =>   $siblings_last_name ?? NULL,
                      'going_to_grade'           =>   $going_to_grade[$i],
                      'photo'                    =>   $img_name,
                      'dob'                      =>   $dob[$i],
                      'place_of_birth'           =>   $place_of_birth[$i],
                      'student_nationality'      =>   $nationality[$i],
                      'religion'                 =>   $religion[$i],
                      'blood_group'              =>   $blood_group[$i],
                      'passport_no'              =>   $passport_no[$i],
                      'passport_place_of_issue'  =>   $passport_place_of_issue[$i],
                      'passport_date_of_issue'   =>   $passport_issue_date[$i],
                      'passport_date_of_expiry'  =>   $passport_expiry_date[$i],
                      'civil_id_no'              =>   $civil_id_no[$i],
                      'civil_id_date_of_expiry'  =>   $civil_id_expiry_date[$i],
                      'specify_child_illlness'   =>   $illlness[$i] ?? NULL,
                      'status'                   =>   'IA',
                      'created_at'               =>   date('Y-m-d h:i:s', time())
                   );


               }
               /* work fine with insert 
                $student_save = StudentDetail::insert($student_array);
               */ 
              //error with create
               $student_save = StudentDetail::create($student_array);

The model code is:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class StudentDetail extends Model
{
     protected $table = 'student_details';

     protected $guarded = [];
}

Error Message: jquery-2.2.4.min.js:4 POST http://127.0.0.1:8000/new-registration/store 500 (Internal Server Error)

enter image description here

1

There are 1 best solutions below

0
Matthew Turland On

You're treating $student_array as a running collection of rows you're adding, where you need to pass an individual row to StudentDetail::create().

I think that instead of this:

$student_save = StudentDetail::create($student_array);

You're intending to do this:

$student_save = StudentDetail::create($student_array[$i]);

i.e. using $i to reference the last row you added to $student_array and the row that you're wanting to pass to StudentDetail::create().