public function saveOrder(Request $request)
{
$sid = explode(",", $request->subcat_id);
$quan = explode(",", $request->quantity);
$amnt = explode(",", $request->amount);
$c = count($sid);
for($i=0; $i < $c; $i++) {
$order = new Order();
$order->quantity = $quan[$i];
$order->amount = $amnt[$i];
$order->subcat_id = $sid[$i];
$order->address = $request->address;
$order->payment_mode = $request->payment_mode;
$order->order_no = $this->generateOrderNR();
if ($request->hasFile('image')) {
echo "in".$i;
$file = $request->file('image');
$file_name = time() . rand(1, 999) . '.' . $file->getClientOriginalExtension();
$file->move(base_path('images/orders/'), $file_name);
$order->image = $file_name;
}
if ($order->save()) {
echo "Order";
//return response()->json(['status' => 200, 'msg' => 'Order Placed Successfully']);
} else
echo "Error";
//return response()->json(['status' => 200, 'msg' => 'Error Occured']);
}
}
in0 Order in1{ "message": "The file "277762619_112136134785398_1736805384468006822_n.jpg" was not uploaded due to an unknown error."
Why this is error is coming up every time, because of this error in database only 1 row is saved and another row isn't saved when is send data like this in postman through api:-
address: Lucknow,
quantity: 1,1,
amount: 150,162,
payment_mode: CASH,
subcat_id: 8,12
3 Answers 3
The second file you wanna upload is too big maybe.
Please check php.ini if the file size exceeds the upload_max_filesize.
Then, increase the size like 1000M or something.
Then restart apache.
Comments
Instead of loop you can pass an array in the insert or create method. Check below code sample :
AppSettings::insert([
[
'name'=>'mail_host',
'type'=>$emailsettingstype->id,
'value'=>'',
],
[
'name'=>'mail_port',
'type'=>$emailsettingstype->id,
'value'=>'',
],
[
'name'=>'mail_username',
'type'=>$emailsettingstype->id,
'value'=>'',
],
]);
Comments
use this instead move()
$file = $request->file('image');
$file_name = time().rand(1, 999).'.'.$file->getClientOriginalExtension();
File::copy($file->getPathname(), base_path('images/orders/'.$file_name));
$sid, so you are creating 2xOrders, your file upload code only works for 1 file:$request->file('image'). So it will be processed for the first iteration, and then the same file will be processed again for the 2nd iteration. That throws the error. You either need to process a different file, or do it only once.