如何在Laravel中将数据从私有功能传递到公共功能?

时间:2020-08-01 05:44:09

标签: php laravel

我有私人和公共职能。私有函数提取从表单发送的数据,而公共函数执行检出。

    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']);
    }

我收到此错误

函数的参数太少 App \ Http \ Controllers \ PayPalController :: projectData(),传入的0 /var/www/CareerHouse/app/Http/Controllers/PayPalController.php在线 42,恰好是1

我不知道自己在做什么错,如何纠正呢?

2 个答案:

答案 0 :(得分:2)

由于付款是公共方法,并且它会重定向,因此很可能是由路线调用的。在这种情况下,您可以按以下步骤在付款中注入请求实例:

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']);
    }

答案 1 :(得分:0)

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']);
    }

仅当您来自请求时才可以使用依赖项注入,而不能从请求的函数中调用另一个函数时使用依赖注入,如果要这样做,请尝试按我的方式进行,或者将ur请求添加到处理此问题的主控制器上,传递给其他方法。