问题
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