Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

anilkumarthakur60's avatar

pass data from one controller to another

1st controller

class PaymentController extends Controller
{
    public function paymentprocess(paymentRequest $request){
        $name=$request->name;
		$email=$request->email;
$phone=$request->phone;
$city=$request->city;
$address=$request->address;
$payment=$request->payment;

        if ($payment=='esewa') {
            toastr()->success('payment method esewa');
           return view('pages.payment.esewa')->with('cartcontent',Cart::content());
        }
        elseif($request->payment=='khalti') {
            toastr()->success('payment method khalti');
            return view('pages.payment.khalti');
        }
        elseif($request->payment=='cashondelivery'){
            toastr()->success('payment method cash ondelivery');
            return view('pages.payment.cashondelivery');

        }

    }
    //
}

2nd controller

class EsewaController extends Controller
{
  public function success(Request $request){

        $setting=Setting::first();

        foreach (Cart::content() as $carts) {
            $rowid=$carts->rowId;
            }

        if(isset($request->oid) && isset($request->amt) && isset($request->refId)){


               $refId=$request->refId;
               if($request->session()->has('coupon')){
                $balance =$request->session()->get('coupon')['balance'];
                    $amt=number_format($balance*(1+$setting->vat/100)+$setting->shipping_charge,2);

                 }else{
                     $amt=Cart::subtotal()*(1+$setting->vat/100)+$setting->shipping_charge;
                 }

            $url = "https://uat.esewa.com.np/epay/transrec";

            $data =[

                    'amt'=>$amt,
                    'rid'=>$refId,
                    'pid'=>$rowid,
                    'scd'=> 'EPAYTEST'
                ];


                $curl = curl_init($url);
                curl_setopt($curl, CURLOPT_POST, true);
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
                curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
                $response = curl_exec($curl);

                curl_close($curl);


                if(strpos($response,'Success')==true){
                    $user_id=Auth::id();
                      Order::create(['user_id'=>$user_id,'amt'=>$request->amt,'oid'=>$request->oid,'refId'=>$request->refId,'status'=>1,'payment_method'=>'esewa']);
                     // $info=$this->paymentprocess(); i can access paymentprocess method data here i can insert here
                     // Shipping::create(['order_id'=>$info->oid,'name'=>$info->name,'phone'=>$info->phone,'email'=>$info->email,'city'=>$info->city,'address'=>$info->address,'payment'=>'esewa']);
                      toastr()->success('payment success and your order has been placed ');
                      return redirect(route('index'));

                }else{
                  toastr()->warning('Transaction failled try again');
                  return view('pages.payment')->with('cartconten',Cart::content());
                }

            }


    }
}

i want to get data from the controller PaymentController @paymentprocess method to EsewaController @success method in paymentprocess method data is being sent from the form as name email phone address city and these data is needed to insert in the shipping table after the successful payment . in success method i am geeting data from the online payment gateway so i need to access the data from the paymentprocess method how can i do that help me

0 likes
4 replies
gokulkhatiwada's avatar

You will need an autogenerated invoice number for each order and pass that PID to esewa for the transaction, after you receive a response from esewa , verify the payment status and get the order from the database using that PID you receive from esewa. Now you have both esewa response and your order model

anilkumarthakur60's avatar

order model is working fine ... need to insert shipping detail after successful payment

anilkumarthakur60's avatar

so i need to pass the data from first controller to second controller so that i can push that data from second controller to database

anilkumarthakur60's avatar
Level 6

in 1st controller i put that data in session and access that data in second controller

Please or to participate in this conversation.