这是我要插入的表的模型: protected $table ="final_schedule"; public $timestamps = false; protected $fillable = [ 'CWID', //varchar 'CRN', //int 'Date_Registered'//date ];这是我现在拥有的插入功能:<?phpnamespace App\Http\Controllers\Student;use App\Http\Controllers\Controller;use Illuminate\Http\Request;use App\Models\finalScheduleModel;class finalScheduleController extends Controller{ public function insert(Request $request){ $finalSchedule = new finalScheduleModel; $finalSchedule->CWID=$request->input('CWID'); $finalSchedule->CRN=$request->input('CRN'); $finalSchedule->Date_Registered=$request->input('Date_Registered'); $finalSchedule->save(); }}路线: Route::post('insert/', 'Student\finalScheduleController@insert');这是我希望它处理的数据:[ { "CWID":"C38475920", "CRN":345627, "Date_Registered":"2020-04-02" }, { "CWID":"C38475920", "CRN":678595, "Date_Registered":"2020-04-02" }, { "CWID":"C38475920", "CRN":473876, "Date_Registered":"2020-04-02" }]至少我认为是。上面的数据结构方式和这个一样吗?(3) [{…}, {…}, {…}]0: {…}1: {…}2: {…}
1 回答
喵喵时光机
TA贡献1846条经验 获得超7个赞
您甚至可以通过一次 insert 调用将多条记录插入表中
$items = $request->all();
$items = [
[
"CWID"=>"C38475920",
"CRN"=>345627,
"Date_Registered"=>"2020-04-02"
],
[
"CWID"=>"C38475920",
"CRN"=>678595,
"Date_Registered"=>"2020-04-02"
],
[
"CWID"=>"C38475920",
"CRN"=>473876,
"Date_Registered"=>"2020-04-02"
]
]
finalScheduleModel::insert($items);
或者
DB::table('final_schedule')->insert($items);
- 1 回答
- 0 关注
- 91 浏览
添加回答
举报
0/150
提交
取消