Untitled

 avatar
unknown
php
2 years ago
1.6 kB
5
Indexable
public function PurchaseStore(Request $request)
    {
        if ($request->category_id == null) {
            $notification = [
                'message' => 'Sorry you did not select any item',
                'alert-type' => 'error'
            ];
            return redirect()->back()->with($notification);
        }
    
        DB::transaction(function () use ($request) {
            $group = uniqid(); // Generate a unique identifier for the group
    
            foreach ($request->category_id as $index => $category_id) {
                $purchase = new Purchase();
                $purchase->date = date('Y-m-d', strtotime($request->date[$index]));
                $purchase->customer_id = $request->customer_id[$index];
                $purchase->category_id = $category_id;
                $purchase->product_id = $request->product_id[$index];
                $purchase->serialnumber = $request->serialnumber[$index];
                $purchase->imei = $request->imei[$index];
                $purchase->phone_no = $request->phone_no[$index];
                $purchase->created_by = Auth::user()->id;
                $purchase->status = '0';
                $purchase->group = $group; // Assign the same group identifier to each purchase
    
                $purchase->save();
            }
        });
        $notification = [
            'message' => 'Purchases saved successfully!',
            'alert-type' => 'success'
        ];
        return redirect()->route('purchase.all')->with($notification);
    }
Editor is loading...