config/app/Http/Controllers/OrderController.php
/**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
Store a newly created resource in storage.
Store a newly created resource in storage .
public function store(Request $request)
    {
        $request->validate([
            'product_id'=>'required',
            'quantity'=>'required',
            'price'=>'required',
            'total_amount'=>'required',
            'customer_mobile'=>'max:15',
            'payment_method'=>'required',
            'paid_amount'=>'required',
        ],[
        ]);
        $insert = DB::transaction(function () use($request) {
            $orders = new Order;
            $orders->name = $request->customer_name;
            $orders->mobile = $request->customer_mobile;
            $orders->save();
            $order_id = $orders->id;
            for($product_id = 0; $product_id < count($request->product_id); $product_id++){
                $order_details = new OrderDetail;
                $order_details->order_id = $order_id;
                $order_details->product_id = $request->product_id[$product_id];
                $order_details->unit_price = $request->price[$product_id];
                $order_details->quantity = $request->quantity[$product_id];
                $order_details->discount = $request->discount[$product_id];
                $order_details->amount = $request->total_amount[$product_id];
                $order_details->save();
                DB::table('products')->where('id',$request->product_id[$product_id])
                ->decrement('quantity',$request->quantity[$product_id]);
            }
            $transactions = new Transaction;
            $transactions->order_id = $order_id;
            $paid_amount = $transactions->paid_amount = $request->paid_amount;
            $change_balance = $transactions->change_balance = $request->change_balance;
            $transactions->transaction_amount = $paid_amount - $change_balance;
            $transactions->payment_method = $request->payment_method;
            $transactions->user_id = auth()->user()->id;
            $transactions->transaction_date = Carbon::now()->toDateTimeString();
            $transactions->save();
            $products = Product::all();
            $order_details = OrderDetail::where('id',$order_id)->get();
            $orderedBy = Order::where('id',$order_id)->get();
            return view('admin.orders.index',[
                'products'=>$products,
                'order_details'=>$order_details,
                'customer_orders'=>$orderedBy
            ]);
        });
        if($insert){
            Session::flash('success','Your Order Inserted Successfully !');
            return redirect('/admin/orders');
        }
        else{
            Session::flash('error','Your Order Fails to Inserted !');
            return redirect('/admin/orders'); 
        }
    }
public function store ( Request $ request ) { $ request -> validate ( [ 'product_id' => 'required' , 'quantity' => 'required' , 'price' => 'required' , 'total_amount' => 'required' , 'customer_mobile' => 'max:15' , 'payment_method' => 'required' , 'paid_amount' => 'required' , ] , [ ] ) ; $ insert = DB :: transaction ( function ( ) use ( $ request ) { $ orders = new Order ; $ orders -> name = $ request -> customer_name ; $ orders -> mobile = $ request -> customer_mobile ; $ orders -> save ( ) ; $ order_id = $ orders -> id ; for ( $ product_id = 0 ; $ product_id < count ( $ request -> product_id ) ; $ product_id ++ ) { $ order_details = new OrderDetail ; $ order_details -> order_id = $ order_id ; $ order_details -> product_id = $ request -> product_id [ $ product_id ] ; $ order_details -> unit_price = $ request -> price [ $ product_id ] ; $ order_details -> quantity = $ request -> quantity [ $ product_id ] ; $ order_details -> discount = $ request -> discount [ $ product_id ] ; $ order_details -> amount = $ request -> total_amount [ $ product_id ] ; $ order_details -> save ( ) ; DB :: table ( 'products' ) -> where ( 'id' , $ request -> product_id [ $ product_id ] ) -> decrement ( 'quantity' , $ request -> quantity [ $ product_id ] ) ; } $ transactions = new Transaction ; $ transactions -> order_id = $ order_id ; $ paid_amount = $ transactions -> paid_amount = $ request -> paid_amount ; $ change_balance = $ transactions -> change_balance = $ request -> change_balance ; $ transactions -> transaction_amount = $ paid_amount - $ change_balance ; $ transactions -> payment_method = $ request -> payment_method ; $ transactions -> user_id = auth ( ) -> user ( ) -> id ; $ transactions -> transaction_date = Carbon :: now ( ) -> toDateTimeString ( ) ; $ transactions -> save ( ) ; $ products = Product :: all ( ) ; $ order_details = OrderDetail :: where ( 'id' , $ order_id ) -> get ( ) ; $ orderedBy = Order :: where ( 'id' , $ order_id ) -> get ( ) ; return view ( 'admin.orders.index' , [ 'products' => $ products , 'order_details' => $ order_details , 'customer_orders' => $ orderedBy ] ) ; } ) ; if ( $ insert ) { Session :: flash ( 'success' , 'Your Order Inserted Successfully !' ) ; return redirect ( '/admin/orders' ) ; } else { Session :: flash ( 'error' , 'Your Order Fails to Inserted !' ) ; return redirect ( '/admin/orders' ) ; } }
Store a newly created resource in storage.
Store a newly created resource in storage .
// insert all requested data into orders table,order_details table and transactions table // Order Model(insert into orders table) // OrderDetail Model(insert data into order_details table) // Transaction Model(insert data into transactions table) // Last order history