0
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
Don't Panic
14.1k5 gold badges35 silver badges55 bronze badges
asked Jun 16, 2022 at 4:46
5
  • error is not about database its about image upload Commented Jun 16, 2022 at 4:50
  • I searched for your error and found this question. The answers suggest your file upload code is running twice. Looking again at your code, yes it is - even if you have 2x $sid, so you are creating 2x Orders, 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. Commented Jun 16, 2022 at 5:54
  • 1
    Does this answer your question? Laravel: The file was not uploaded due to an unknown error Commented Jun 16, 2022 at 5:54
  • @Don'tPanic Yes image insertion goes 2 times when there is only one image comes from api. And when i save only one order everything works fine but more than 1 order creation this error thrown and this didn't solve from your given answer. I think maybe i need to send more than 1 image or i need to check first how many images are there Commented Jun 16, 2022 at 6:14
  • Yes, that is exactly what I said: "You either need to process a different file, or do it only once." If you are allowing multiple Orders in one submission, you either need to allow multiple files on your form, or process just one, ie set a flag when you have processed it and don't do it again on the next iteration. The issue is the same as the duplicate :-) Commented Jun 16, 2022 at 6:22

3 Answers 3

0

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.

answered Jun 16, 2022 at 4:51
Sign up to request clarification or add additional context in comments.

Comments

0

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'=>'',
 ],
]);
answered Jun 16, 2022 at 4:51

Comments

0

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));
answered Jun 16, 2022 at 6:59

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.