如何解决类型错误:Laravel中的函数参数太少 [英] How To Solve Type error: Too few arguments to function In Laravel

查看:468
本文介绍了如何解决类型错误:Laravel中的函数参数太少的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个网站.在这个网站上,我创建了一个注册表.因此,我创建了一个名为saveInvoice的函数以将所有数据插入数据库.之后,我创建了另一个名为sendemail的函数.我已经从saveInvoice传递了2个参数到sendmail函数,像这样-

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 -

$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

如何解决此问题?

这是saveInvoice函数.

Here is saveInvoice function.

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

    }

这是sendemail功能.

Here is sendemail function.

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('kistlakall@gmail.com', 'CheapEfares');
        });
        return Redirect::back();
    }

路线.

Route::Post('invoice/addinvoice', [
    'uses' => 'InvoicesController@saveInvoice',
    'as' => 'invoice.save'
]);

Route::get('sendemail','InvoicesController@sendemail')->name('sendemail');

推荐答案

从路由调用函数sendemail()时.它仅向其传递一个参数,例如:

As you are calling function sendemail() from route. It's passing only one parameter to it like:

sendemail($request);

$total变量根本不在功能sendemail()中使用.因此,将其删除或使其变为可选,例如:

Also $total variable not being used in function sendemail() at all. So remove it or make it optional like:

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('kistlakall@gmail.com', 'CheapEfares');
    });
    return Redirect::back();
}

您还可以通过路径传递总变量.

Also you can pass total variable through routes as well.

Route::get('sendemail/{total}','InvoicesController@sendemail')->name('sendemail');

这篇关于如何解决类型错误:Laravel中的函数参数太少的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆