在下面的示例代码中,我想每次使用作业获取所有保存的学生ID。
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);
模型代码是:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class StudentDetail extends Model
{
protected $table = 'student_details';
protected $guarded = [];
}
错误信息:jquery-2.2.4.min.js:4 POST http:/127.0.0.1:8000new-registrationstore。 500(内部服务器错误)
你在处理 $student_array
作为你正在添加的行的运行集合,你需要将一个单独的行传递给 StudentDetail::create()
.
我觉得与其这样,不如这样
$student_save = StudentDetail::create($student_array);
你打算这样做:
$student_save = StudentDetail::create($student_array[$i]);
即使用... $i
来引用我们添加到 $student_array
和你要传递给你的行 StudentDetail::create()
.