我正在向控制器发送数据数组,但它只将 1 行保存到数据库,我需要保存多行(取决于数组长度)。
代码
controller
public function store(Request $request)
{
$user = Auth::guard('api')->user();
$transit = new Transit;
foreach($request->input('serials') as $serial){ //looping my serials array for each row
if(!empty($serial['barcode_id'])) { // filter out empty objects from array
$barcode = Barcode::where('serial_number', $serial['barcode_id'])->orWhere('u_serial_number', $serial['barcode_id'])->first();
$transit->barcode_id = $barcode['id'];
$transit->transNu = mt_rand(1000000000, 9999999999);
}
}
$transit->from_id = $request->input('from_id');
$transit->to_id = $request->input('to_id');
$transit->sender_id = $user->id;
$transit->description = $request->input('description');
if($transit->save()){
DB::table('outlet_products')->where('barcode_id', $transit->barcode_id)->update(['outlet_id' => null]);
}
return response()->json([
'data' => $transit,
'message' => 'Transit data saved successfully.'
]);
}
截屏
request data
stored data
慕容森