Home > OS >  Store Multiple Data From Api in Laravel 8
Store Multiple Data From Api in Laravel 8

Time:06-16

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

CodePudding user response:

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.

CodePudding user response:

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'=>'',
    ],
]);
  • Related