如何解决类型错误:Laravel 中无法运行的参数太少
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50809510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How To Solve Type error: Too few arguments to function In Laravel
提问by Amithash
I am creating a web site. In this web site, I have created a registration form. So , I have created a function called saveInvoice to insert all the data into the database. After that I created another function called sendemail. I have passed 2 arguments to sendmail function from saveInvoice like this -
我正在创建一个网站。在这个网站上,我创建了一个注册表。因此,我创建了一个名为 saveInvoice 的函数来将所有数据插入到数据库中。之后,我创建了另一个名为 sendemail 的函数。我已经像这样从 saveInvoice 向 sendmail 函数传递了 2 个参数 -
$this->sendemail($request, $total);
But , when I click Submit button, it gives me this error -
但是,当我单击提交按钮时,它给了我这个错误 -
Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_RECOVERABLE_ERROR)
Type error: Too few arguments to function App\Http\Controllers\InvoicesController::sendemail(), 1 passed and exactly 2 expected
How can I Fix this ??
我怎样才能解决这个问题 ??
Here is saveInvoice function.
这是 saveInvoice 函数。
public function saveInvoice(Request $request)
{
if (Auth::user()) {
$settings = Setting::find(1);
$invoiceNo = $settings->invoiceprefix . '' . str_pad($settings->invoiceno, $settings->invoicepadding, 0, STR_PAD_LEFT);
$Qty = $request->input('Qty');
$price = $request->input('price');
$total = $Qty * $price;
$invoice = new Invoice();
$invoice->invoicereference = $invoiceNo;
$invoice->firstname = $request->fname;
$invoice->save();
if ($invoice == null) {
return redirect()->back()->with('msg', 'invalid request');
} else {
$this->sendemail($request, $total);
return redirect()->route('invoice.preview', $invoiceNo);
}
}
}
Here is sendemail function.
这是sendemail功能。
public function sendemail(Request $request, $total)
{
$invoiceNo = $request->input('invoiceNo');
$fname = $request->input('fname');
$sendemail = $request->input('email');
$data = [];
$data['invoiceNo'] = $invoiceNo;
$data['fname'] = $fname;
$data['total'] = $total;
$data['sendemail'] = $sendemail;
Mail::send(['html' => 'mail'], $data, function ($message) use ($data) {
$message->to($data["sendemail"], 'TicketBooker')->subject
('CheapEfares Order Invoice');
$message->from('[email protected]', 'CheapEfares');
});
return Redirect::back();
}
Routes.
路线。
Route::Post('invoice/addinvoice', [
'uses' => 'InvoicesController@saveInvoice',
'as' => 'invoice.save'
]);
Route::get('sendemail','InvoicesController@sendemail')->name('sendemail');
回答by Lovepreet Singh
As you are calling function sendemail()
from route. It's passing only one parameter to it like:
当您sendemail()
从路由调用函数时。它只向它传递一个参数,例如:
sendemail($request);
Also $total
variable not being used in function sendemail()
at all. So remove it or make it optional like:
此外$total
变量没有在函数中使用sendemail()
的。因此,将其删除或使其成为可选的,例如:
public function sendemail(Request $request, $total = "") {
$invoiceNo = $request->input('invoiceNo');
$fname = $request->input('fname');
$sendemail = $request->input('email');
$data = [];
$data['invoiceNo'] = $invoiceNo;
$data['fname'] = $fname;
$data['total'] = empty($total) ? 0 : $total;
$data['sendemail'] = $sendemail;
Mail::send(['html' => 'mail'], $data, function ($message) use ($data) {
$message->to($data["sendemail"], 'TicketBooker')->subject
('CheapEfares Order Invoice');
$message->from('[email protected]', 'CheapEfares');
});
return Redirect::back();
}
Also you can pass total variable through routes as well.
您也可以通过路由传递总变量。
Route::get('sendemail/{total}','InvoicesController@sendemail')->name('sendemail');
回答by Rafael M. G.
Put a default value, so it will not be necessary to pass the argument to the function
放置一个默认值,因此不需要将参数传递给函数
public function sendemail(Request $request = null, $total = '') {}
回答by Nitish Patra
Do not use Request in the sendemail() function definition, as you are already passing $request and $total into it. Request $request will try to instantiate a new Request Object and will look for it, whereas you are already passing a simple variable. Use it like this
不要在 sendemail() 函数定义中使用 Request ,因为您已经将 $request 和 $total 传递给它。Request $request 将尝试实例化一个新的 Request 对象并寻找它,而您已经传递了一个简单的变量。像这样使用它
public function sendemail($request, $total){ .. .. }
公共函数 sendemail($request, $total){ .. .. }