如何解决类型错误:在Laravel

时间:2018-06-12 05:00:01

标签: php laravel

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

$this->sendemail($request, $total); 

但是,当我点击提交按钮时,它会给我这个错误 -

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函数。

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功能。

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

3 个答案:

答案 0 :(得分:1)

当您从路线呼叫功能sendemail()时。它只传递一个参数,如:

sendemail($request);

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

您也可以通过路径传递总变量。

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

答案 1 :(得分:1)

设置默认值,因此无需将参数传递给函数

public function sendemail(Request $request = null, $total = '') {}

答案 2 :(得分:0)

不要在sendemail()函数定义中使用Request,因为您已经将$ request和$ total传递给它。 Request $ request将尝试实例化一个新的Request Object并寻找它,而你已经传递了一个简单的变量。 像这样使用它

public function sendemail($ request,$ total){     ..     .. }