dimanche 24 juin 2018

use cookie to store and retrieve order data in laravel

use cookie to store and retrieve order data in laravel

I want to use cookie to store and retrieve order data in store order:

public function store(Request $request, $serviceId) {

        $request->validate([
            'company_id' => 'required',
            'user_id' => 'required',
            'individual_count' => 'required',
            'date' => 'required',
            'time' => 'required',
            'total_price' => 'required',
            'is_home' => 'required',
        ]);

        $request['date'] = date('Y-m-d H:i:s', strtotime($request->date . $request->time));
        $request['total_price'] = explodeBySpace($request->total_price)[0];
        $request['service_id'] = Hashids::decode($serviceId)[0];

        session([ 'totalOrderPrice' => $request['total_price'] ]);
        session([ 'companyName'     => $request->company_name ]);
        session([ 'individualCount' => $request->individual_count ]);
        session([ 'orderDate'       => $request['date'] ]);

//        dd($request->all());
        $created = Orders::create($request->all());

        if ($created) {

            session(['orderId' => $created->id]);

            Cookie::make('orderId', $created->id, 180);  // ?

            return redirect()->route('payment.method');
        }

        return redirect()->route('web.orders.create', $serviceId)->with('alert', 'error');
    }

to retrieve order data for payment operation :

 public function storeReceipt(Request $request, $method) {

        $request->validate([

            'price' => 'required|numeric',
        ]);

        $request['order_id'] = $request->cookie('orderId');
        $request['method'] = $method;


        $created = Payment::create($request->all());

        return $created->count() > 0
            ? redirect()->route('home')->with('alert', 'success')
            : redirect()->route('payment/method/create', 'receipt')->with('alert', 'error');
    }

but this error occurs

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'order_id' cannot be null

what is the wrong



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire