【问题标题】:How To Use Function Variables With Another Function如何将函数变量与另一个函数一起使用
【发布时间】:2018-11-20 12:17:31
【问题描述】:

我正在创建网站。在这个网站上,我创建了一个注册表单。用户可以输入详细信息,当用户单击提交按钮时,所有数据都会保存到数据库中。这是我的 saveInvoice 函数。在我的视图页面中,我使用了 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);

            $invoice= new Invoice();
            $invoice->invoicereference=$invoiceNo;
            $invoice->firstname=$request->fname;
            $invoice->lastname=$request->lname;

            $invoice->save();

            $settings->invoiceno=$settings->invoiceno+1;
            $settings->update();

            if($invoice == null){
                return redirect()->back()->with('msg','invalid request');
            }
            else{
                return redirect()->route('invoice.preview',$invoiceNo);
            }
        }

    }

但是,现在我想创建另一个名为 sendemail 的函数。在此功能中,我想使用用户填写该注册表单的用户输入。我可以从上述函数中获取这些请求输入还是有其他方法??

public function sendemail(Request $request) {
$invoiceNo = $request->input('invoiceNo');
            $fname = $request->input('fname');
            $Qty = $request->input('Qty');
            $price = $request->input('price');
            $sendemail = $request->input('email');

            $data = [];
            $data['invoiceNo'] = $invoiceNo;
            $data['fname'] = $fname;
            $data['Qty'] = $Qty;
            $data['price'] = $price;
            $data['sendemail'] = $sendemail;

            Mail::send(['text' => 'mail'], $data, function ($message) use ($data) {
                $message->to($data["sendemail"], 'TicketBooker')->subject
                ('CheapEfares Air Ticket');
                $message->from('kistlakall@gmail.com', 'CheapEfares');
            });
}

【问题讨论】:

  • 你有单独的发送邮件功能的 URL/路由吗?
  • 我想您可以将 Request 对象传递给您的 sendemail 函数。你会尝试这样做吗?你在哪里调用这个函数?
  • @DsRaj - 我有一个单独的发送邮件功能路由。
  • @David - 但是,如何向我的 sendmail 函数请求对象??
  • 表示你想用请求重定向?

标签: php laravel


【解决方案1】:

您可以在 saveInvoice 函数中调用 sendemail 函数,然后将 Request 对象传递给 sendemail 函数,如

public function saveInvoice(Request $request){
//save invoice code

if($invoice == null){
  return redirect()->back()->with('msg','invalid request');
}
else{
  $this->sendemail(Request $request);
  return redirect()->route('invoice.preview',$invoiceNo);
}
}

我假设这两个函数都在同一个类中,或者一个类正在扩展另一个类

【讨论】:

  • 我在没有这样的请求的情况下使用 - $this->sendemail($request);这对我有用..
【解决方案2】:

您可以简单地从您的代码中调用该方法 在该方法返回时,您可以设置响应。

$this->sendemail($request); // $this just because it is on same controller or basecontroller

// For example
public function saveInvoice(Request $request){
    .
    .
    .
    $sendMail = $this->sendemail($request);
    if($sendMail == 'success'){
        // On success
    } else {
        // On Error 
    }
} 
public function sendemail(Request $request) {
    .
    .
    .
    return 'success';
}

【讨论】:

  • 不,现在我什至无法发送电子邮件。当我尝试这个时,它给了我这个,类型错误:函数的参数太少。
  • 您需要为您的额外参数设置一个默认值,如更新的问题stackoverflow.com/q/50809510/5503275 中您需要的 $total = null
  • Yh,当我使用 null 时,它起作用了。但是,现在我收到此错误 - 给定 [] 邮箱中的地址不符合 RFC 2822、3.6.2。 ??
  • 好的,我会检查的。。谢谢你!!
猜你喜欢
  • 1970-01-01
  • 2013-04-22
  • 2017-11-04
  • 1970-01-01
  • 2017-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多