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)
You're treating
$student_arrayas a running collection of rows you're adding, where you need to pass an individual row toStudentDetail::create().I think that instead of this:
You're intending to do this:
i.e. using
$ito reference the last row you added to$student_arrayand the row that you're wanting to pass toStudentDetail::create().