我正在尝试发送带有以下数据集的POST请求:
[
{
"Course_Code":"CIS341",
"Start_Date":"2020-08-22",
"End_Date":"2020-12-02",
"CRN":345627,
"CWID":"C38475920",
"Date_Registered":"2020-04-02"
},
{
"Course_Code":"CIS341",
"Start_Date":"2020-08-22",
"End_Date":"2020-12-02",
"CRN":456392,
"CWID":"C38475920",
"Date_Registered":"2020-04-02"
},
{
"Course_Code":"CIS341",
"Start_Date":"2020-08-22",
"End_Date":"2020-12-02",
"CRN":562940,
"CWID":"C38475920",
"Date_Registered":"2020-04-02"
}
]
但是我只想将每个对象的CRN,CWID和Date_Registered插入我的final_schedule表中:
protected $table ="final_schedule";
public $timestamps = false;
protected $fillable = [
'CWID',
'CRN',
'Date_Registered'
];
这里是我正在使用的插入函数:
public function insert(Request $request){
$CWID->CWID = $request->input('CWID');
$CRN->CRN = $request->input('CRN');
$Date_Registered->Date_Registered = $request->input('Date_Registered');
$items = array('CWID'=>$CWID, 'CRN'=>$CRN, 'Date_Registered'=>$Date_Registered);
finalScheduleModel::insert($items);
}
当我在邮递员中测试此插入函数时,会出现错误:ErrorException: Creating default object from empty value in file
错误指向的行是函数的第一行$CWID->CWID = $request->input('CWID');
我尝试以多种不同的方式编写此函数,但是我一直都在出错。它从不读取正在发送的任何数据。可能会说类似尝试将值CWID,CRN,Date_Registered(?,?,?)插入final_schedule。
尝试一下,看看是否适合您。
public function insert(Request $request){
$array = [];
foreach (request()->all() as $value) {
array_push($array, ['CWID' => $value['CWID'], 'CRN' => $value['CRN'], 'Date_Registered' => $value['Date_Registered']]);
}
DB::table('final_schedule')->insert($array);
}