简体   繁体   English

如何在同一个 controller (laravel) 中的函数之间传递变量?

[英]how can i pass a variable between functions in the same controller (laravel)?

im new to laravel and this is the first time i use stackoverflow so forgive me if i made some mistakes.我是 laravel 的新手,这是我第一次使用 stackoverflow,如果我犯了一些错误,请原谅我。 i need to pass $newOrder to the view(orders.success) in order to be able to print the "receipt" once the order is concluded.我需要将$newOrder传递给视图(orders.success),以便在订单结束后能够打印“收据”。 Nothing seems to work, i have tried with compact/with methods but it's always undefined since $newOrder is declared in another function.似乎没有任何效果,我尝试过使用 compact/with 方法,但它总是未定义,因为$newOrder在另一个 function 中声明。 do you have some tips?你有什么建议吗?

    class BraintreeController extends Controller
{

    public function token(Request $request)
    {

        $gateway = new \Braintree\Gateway([
            'environment' => 'sandbox',
            'merchantId' => 'jgvy755pfvwdcjzx',
            'publicKey' => 'qqpm93srfgwtx6dp',
            'privateKey' => 'd13ce21a7642606db73b12bb1300d3fd'
        ]);

        $clientToken = $gateway->clientToken()->generate();

        if ($request->input('nonce') != null) {
            $request->validate([
                'name' => 'required',
                'last_name' => 'required',
                'phone' => 'required',
                'address' => 'required',
                'email' => 'email:rfc',
            ]);

            //# Storo l'ordine
            $name = $request->input('name');
            $last_name = $request->input('last_name');
            $address = $request->input('address');
            $phone = $request->input('phone');
            $email = $request->input('email');
            $arr_id = $request->input('arr_id');
            $arr_quant = $request->input('arr_quant');
            $delivery_fee = $request->input('delivery_fee');
            //# Recupero tutti i piatti dell'ordine per calcolarne il totale
            $dishes = Dish::findMany($arr_id);
            $arrayLength = count($arr_id);
            $amount = 0;
            for ($i = 0; $i < $arrayLength; $i++) {
                $amount +=  $dishes[$i]->price * $arr_quant[$i];
            }
            $amount += $delivery_fee;

            //#


            $newOrder = new Order();
            $newOrder->status = 1;
            $newOrder->address = $address;
            $newOrder->user_name = $name;
            $newOrder->user_surname = $last_name;
            $newOrder->phone = $phone;
            $newOrder->email = $email;
            $newOrder->total = $amount;
            $newOrder->save();

            // // storo l'array di IDs


            // Ciclo una volta per ogni piatto contenuto nell'ordine, salvo la relazione e la sua quantità
            for ($i = 0; $i < $arrayLength; $i++) {
                $dish_id = $arr_id[$i];
                // Scrive nella tabella pivot dopo aver creato la relazione
                $newOrder->dishes()->attach([$dish_id => ['quantity' => $arr_quant[$i]]]);
                //
            }

            //#


            var_dump($request->input('nonce'));
            $nonceFromTheClient = $request->input('nonce');
            $gateway->transaction()->sale([
                'amount' => $amount,
                'paymentMethodNonce' => $nonceFromTheClient,
                'options' => [
                    'submitForSettlement' => True
                ]
            ]);


            Mail::to($email)->send(new PaymentConfirmationMail());
            return view('orders.success');
        }


        return view('orders.braintree', ['token' => $clientToken]);
    }

    public function success(Request $request)
    {
        return view('orders.success');
    }
}

Try this尝试这个

Mail::to($email)->send(new PaymentConfirmationMail());
return view('orders.success',['newOrder' => $newOrder]);

Or change params on success function like this或者像这样更改成功 function 的参数

public function success($newOrder)
{
    return view('orders.success', ['newOrder' => $newOrder]);
}

And return from previews function并从预览中返回 function

Mail::to($email)->send(new PaymentConfirmationMail());
return $this->success($newOrder);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM