How do I pass data from private function to public function in Laravel?

纵饮孤独 提交于 2021-02-11 15:21:11

问题


I have a private and public function. The private function fetches data sent from a form and public function performs checkout.

    private function projectData(Request $request){
                // dd($request->all());
  //    $item = [];
        // $datat = array_map(function($item){
        //  return [
        //      'name'=>$request->project_id,
        //      'price'=>$request->budget,
        //      'desc'=>'Deposit',
        //      'qty'=>1
        //  ];
        // }, $item);
        $data = [
                'items'=>[
                    'name'=>'Project No.'.$request->project_id
                ],
                'invoice_id' => uniqid(),
                'invoice_description' => "Payment for Project No.".$request->project_id." Amount ".$request->budget,
                'return_url' => route('payment.success'),
                'cancel_url' => route('payment.cancel'),
                'total' => $request->budget 
        ];  
        // dd($data);

        return $data;
    }
        /**
     * Responds with a welcome message with instructions
     *
     * @return \Illuminate\Http\Response
     */
    public function payment()    {
        $data = $this->projectData();
        $provider = new ExpressCheckout;
        $response = $provider->setExpressCheckout($data);
        dd($response);
        // $response = $provider->setExpressCheckout($data, true);

        return redirect($response['paypal_link']);
    }

I am getting this error

Too few arguments to function App\Http\Controllers\PayPalController::projectData(), 0 passed in /var/www/CareerHouse/app/Http/Controllers/PayPalController.php on line 42 and exactly 1 expected

I don't know what I am doing wrong and how can I rectify it?


回答1:


Since payment is the public method & it redirects, most probably it's called by a route. In this case, you can inject the request instance within payment as following:

public function payment(Request $request)    {
        $data = $this->projectData($request);
        $provider = new ExpressCheckout;
        $response = $provider->setExpressCheckout($data);
        dd($response);
        // $response = $provider->setExpressCheckout($data, true);

        return redirect($response['paypal_link']);
    }



回答2:


private function projectData(){
$request=request();
                // dd($request->all());
  //    $item = [];
        // $datat = array_map(function($item){
        //  return [
        //      'name'=>$request->project_id,
        //      'price'=>$request->budget,
        //      'desc'=>'Deposit',
        //      'qty'=>1
        //  ];
        // }, $item);
        $data = [
                'items'=>[
                    'name'=>'Project No.'.$request->project_id
                ],
                'invoice_id' => uniqid(),
                'invoice_description' => "Payment for Project No.".$request->project_id." Amount ".$request->budget,
                'return_url' => route('payment.success'),
                'cancel_url' => route('payment.cancel'),
                'total' => $request->budget 
        ];  
        // dd($data);

        return $data;
    }
        /**
     * Responds with a welcome message with instructions
     *
     * @return \Illuminate\Http\Response
     */
    public function payment()    {
        $data = $this->projectData();
        $provider = new ExpressCheckout;
        $response = $provider->setExpressCheckout($data);
        dd($response);
        // $response = $provider->setExpressCheckout($data, true);

        return redirect($response['paypal_link']);
    }

You can use dependency injection only when you are coming from a request not when calling another function from a requested function, if you are to do this try the way i did it or add ur request to the main controller that handles this and pass to the other method.



来源:https://stackoverflow.com/questions/63202191/how-do-i-pass-data-from-private-function-to-public-function-in-laravel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!