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

laden's avatar
Level 1

how to pass a variable from a function to another function?

I am trying to setup a stripe payment option. The problem I am stuck at atm is that I want to do this as a 2-step process, where I first validate all the information filled out in the form and redirect to a new view where I take care of the payment and storing information in the database.

Problem is I cant figure out how to pass the variables in my first function to the second function.

This is the first function, for the validation process:

public function validator(Request $request)
{
    $price = Course::where('kursId', request('courseId'))->value('pris')*100;
    try {
        $data = $request->validate([
            'courseEvent' => 'required|max:11',
            'enrollmentFirstName' => 'required|max:255',
            'enrollmentLastName' => 'required|max:255',
            'enrollmentBirthDate' => 'required|max:255',
            'enrollmentAddress' => 'required|max:255',
            'enrollmentAddressNr' => 'required|max:255',
            'enrollmentEmail' => 'required|max:255',
            'enrollmentPhone' => 'required|max:255',
            'companyName' => 'required|max:255',
        ]);  
    } catch (\Exception $ex) {
        \Session::flash('flash_message_feil','Ops, det er noe som ikke er fylt riktig ut i skjemaet!!!');
        return redirect()->back();
    }
    return view('enrollmentPrivate.payment', compact('price', 'data'));
}

in my view I can use the variables just fine like this:

<form action="/charge" method="post">
    {{ csrf_field() }}
    <script
        src="https://checkout.stripe.com/checkout.js" class="stripe-button"
        data-key="{{ env('STRIPE_PUB_KEY') }}"
        data-amount="{{ $price }}"
        data-label="Betal med kort"
        data-name="Stripe Demo"
        data-image="https://stripe.com/img/documentation/checkout/marketplace.png"
        data-locale="auto"
        data-currency="nok">
    </script>
</form>

But when I try to use them in the next function for the actual charging, I get "undefined variable" error. second function:

public function charge(Request $request)
{
    dd($data);
}

This is my routes: (dont know how relevant they are tho)

Route::post('/validate', 'EnrollmentPrivateController@validator');
Route::post('/charge', 'EnrollmentPrivateController@charge');

I will be very grateful for any help I can get with this! :)

0 likes
4 replies
WW's avatar

dd(request());

Works for me. You can also do dd(request()->toArray()), in order to display input fields only. Hope that helps!

arthurvillar's avatar

Take a look at your code. What is $data? You are passing into the method charge() an instance of the Request class as a variable called $request. The method charge() knows nothing about $data so it will tell you just that (undefined variable).

If I understand correctly, you first reach out to the route /validate, then go back to the browser and then go to the route /charge.

On /charge, if you do this instead

public function charge(Request $request)
{
    dd($request);
}

What do you get?

Gurudevbox+laracast@gmail.com's avatar

it's hard to gauge what you're trying to do in the enrollmentPrivate.payment view. if you can give rest of the code that might help.

like @arthurvillar says the charge function does not know what $data is.

without much info, you can use the session to store the value which is not the greatest outcome.

shez1983's avatar

the way i see it you are using stripe... and with them you should be doing is creating an order with pending status.. then when users go to your view they see stripe's form which when entered the data goes to stripe who return you some sort of customer id & redirect to a page you enter in stripe settings which then you take the customer id and charge them there using the info from order.

(theres laracast series here on stripe i suggest you use them)

to directly answer your question in your form i do not see any form fields so when user submits you wont recieve any data (even though u r passing in $request but accessing an imaginary $data.. which can be fixed in TWO ways:

  1. like i mentioned above store all the user's details in an order/transaction marking it as incomplete/pending - then when you go to charge func you do the query for the users' last order with pending state..

  2. in your form add HIDDEN fields (which wont work but thats what you could usually do if the form was hosted by you and you had a multi-page process) and then those variables will be available in $request..

but really you should be following stripe laracast series as you seem to be doing things wrong

Please or to participate in this conversation.