dshmkgq558192365 2018-06-12 05:00
浏览 240
已采纳

如何解决类型错误:在Laravel中运行的参数太少

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

How can I Fix this ??

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

    }

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

Routes.

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

Route::get('sendemail','InvoicesController@sendemail')->name('sendemail');
  • 写回答

3条回答 默认 最新

  • douhang5493 2018-06-12 05:08
    关注

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

    sendemail($request);
    

    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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c