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)
    {
        $this->validate($request,[
            'first_name'=>'string|required',
            'last_name'=>'string|required',
            'address1'=>'string|required',
            'address2'=>'string|nullable',
            'coupon'=>'nullable|numeric',
            'phone'=>'numeric|required',
            'post_code'=>'string|nullable',
            'email'=>'string|required'
        ]);
        if(empty(Cart::where('user_id',auth()->user()->id)->where('order_id',null)->first())){
            request()->session()->flash('error','Cart is Empty !');
            return back();
        }
        $order=new Order();
        $order_data=$request->all();
        $order_data['order_number']='ORD-'.strtoupper(Str::random(10));
        $order_data['user_id']=$request->user()->id;
        $order_data['shipping_id']=$request->shipping;
        $shipping=Shipping::where('id',$order_data['shipping_id'])->pluck('price');
        $order_data['sub_total']=Helper::totalCartPrice();
        $order_data['quantity']=Helper::cartCount();
        if(session('coupon')){
            $order_data['coupon']=session('coupon')['value'];
        }
        if($request->shipping){
            if(session('coupon')){
                $order_data['total_amount']=Helper::totalCartPrice()+$shipping[0]-session('coupon')['value'];
            }
            else{
                $order_data['total_amount']=Helper::totalCartPrice()+$shipping[0];
            }
        }
        else{
            if(session('coupon')){
                $order_data['total_amount']=Helper::totalCartPrice()-session('coupon')['value'];
            }
            else{
                $order_data['total_amount']=Helper::totalCartPrice();
            }
        }
        $order_data['status']="new";
        if(request('payment_method')=='paypal'){
            $order_data['payment_method']='paypal';
            $order_data['payment_status']='paid';
        }
        else{
            $order_data['payment_method']='cod';
            $order_data['payment_status']='Unpaid';
        }
        $order->fill($order_data);
        $status=$order->save();
        if($order)
        $users=User::where('role','admin')->first();
        $details=[
            'title'=>'New order created',
            'actionURL'=>route('order.show',$order->id),
            'fas'=>'fa-file-alt'
        ];
        Notification::send($users, new StatusNotification($details));
        if(request('payment_method')=='paypal'){
            return redirect()->route('payment')->with(['id'=>$order->id]);
        }
        else{
            session()->forget('cart');
            session()->forget('coupon');
        }
        Cart::where('user_id', auth()->user()->id)->where('order_id', null)->update(['order_id' => $order->id]);
        request()->session()->flash('success','Your product successfully placed in order');
        return redirect()->route('home');
    }
public function store ( Request $ request ) { $ this -> validate ( $ request , [ 'first_name' => 'string|required' , 'last_name' => 'string|required' , 'address1' => 'string|required' , 'address2' => 'string|nullable' , 'coupon' => 'nullable|numeric' , 'phone' => 'numeric|required' , 'post_code' => 'string|nullable' , 'email' => 'string|required' ] ) ; if ( empty ( Cart :: where ( 'user_id' , auth ( ) -> user ( ) -> id ) -> where ( 'order_id' , null ) -> first ( ) ) ) { request ( ) -> session ( ) -> flash ( 'error' , 'Cart is Empty !' ) ; return back ( ) ; } $ order = new Order ( ) ; $ order_data = $ request -> all ( ) ; $ order_data [ 'order_number' ] = 'ORD-' . strtoupper ( Str :: random ( 10 ) ) ; $ order_data [ 'user_id' ] = $ request -> user ( ) -> id ; $ order_data [ 'shipping_id' ] = $ request -> shipping ; $ shipping = Shipping :: where ( 'id' , $ order_data [ 'shipping_id' ] ) -> pluck ( 'price' ) ; $ order_data [ 'sub_total' ] = Helper :: totalCartPrice ( ) ; $ order_data [ 'quantity' ] = Helper :: cartCount ( ) ; if ( session ( 'coupon' ) ) { $ order_data [ 'coupon' ] = session ( 'coupon' ) [ 'value' ] ; } if ( $ request -> shipping ) { if ( session ( 'coupon' ) ) { $ order_data [ 'total_amount' ] = Helper :: totalCartPrice ( ) + $ shipping [ 0 ] - session ( 'coupon' ) [ 'value' ] ; } else { $ order_data [ 'total_amount' ] = Helper :: totalCartPrice ( ) + $ shipping [ 0 ] ; } } else { if ( session ( 'coupon' ) ) { $ order_data [ 'total_amount' ] = Helper :: totalCartPrice ( ) - session ( 'coupon' ) [ 'value' ] ; } else { $ order_data [ 'total_amount' ] = Helper :: totalCartPrice ( ) ; } } $ order_data [ 'status' ] = " new " ; if ( request ( 'payment_method' ) == 'paypal' ) { $ order_data [ 'payment_method' ] = 'paypal' ; $ order_data [ 'payment_status' ] = 'paid' ; } else { $ order_data [ 'payment_method' ] = 'cod' ; $ order_data [ 'payment_status' ] = 'Unpaid' ; } $ order -> fill ( $ order_data ) ; $ status = $ order -> save ( ) ; if ( $ order ) $ users = User :: where ( 'role' , 'admin' ) -> first ( ) ; $ details = [ 'title' => 'New order created' , 'actionURL' => route ( 'order.show' , $ order -> id ) , 'fas' => 'fa-file-alt' ] ; Notification :: send ( $ users , new StatusNotification ( $ details ) ) ; if ( request ( 'payment_method' ) == 'paypal' ) { return redirect ( ) -> route ( 'payment' ) -> with ( [ 'id' => $ order -> id ] ) ; } else { session ( ) -> forget ( 'cart' ) ; session ( ) -> forget ( 'coupon' ) ; } Cart :: where ( 'user_id' , auth ( ) -> user ( ) -> id ) -> where ( 'order_id' , null ) -> update ( [ 'order_id' => $ order -> id ] ) ; request ( ) -> session ( ) -> flash ( 'success' , 'Your product successfully placed in order' ) ; return redirect ( ) -> route ( 'home' ) ; }
Store a newly created resource in storage.
Store a newly created resource in storage .
// return $request->all(); // $cart=Cart::get(); // // return $cart; // $cart_index='ORD-'.strtoupper(uniqid()); // $sub_total=0; // foreach($cart as $cart_item){ //     $sub_total+=$cart_item['amount']; //     $data=array( //         'cart_id'=>$cart_index, //         'user_id'=>$request->user()->id, //         'product_id'=>$cart_item['id'], //         'quantity'=>$cart_item['quantity'], //         'amount'=>$cart_item['amount'], //         'status'=>'new', //         'price'=>$cart_item['price'], //     ); //     $cart=new Cart(); //     $cart->fill($data); //     $cart->save(); // } // $total_prod=0; // if(session('cart')){ //         foreach(session('cart') as $cart_items){ //             $total_prod+=$cart_items['quantity']; //         } // } // return session('coupon')['value']; // return $order_data['total_amount']; // dd($order->id); // dd($users);        