使用for循环laravel

时间:2017-07-04 06:04:43

标签: php laravel laravel-5.4

我有一个方法来处理来自动态表单的请求。

public function step2(Request $request)
{
    $this->validate($request, [
      'title.*' => 'required',
      'f_name.*'   =>'required',
      'l_name.*'   =>'required',
      'dob_month.*'   =>'required',
      'dob_day.*'   =>'required',
      'dob_year.*'   =>'required',
      'gender.*'   =>'required',
      'email.*'   =>'required',
      'contact.*'   =>'required',
      'address1.*'   =>'required',
      'address2.*'   =>'required',
      'town.*'   =>'required',
      'state.*'   =>'required',
      'mailbox.*'   =>'required',
      'insurance.*'   =>'required',
    ]);   

$data = array(
    for ($i=0; $i <$request->limit ; $i++){
        "title$i" => $request->title[$i],
        "f_name$i" => $request->f_name[$i],
        if (!isEmpty($request->m_name)) {
            "m_name$i" => $request->m_name[$i],
        }            
        "l_name$i" => $request->l_name[$i],
        "dob_month$i" => $request->dob_month[$i],
        "dob_day$i" => $request->dob_day[$i],
        "dob_year$i" => $request->dob_year[$i],
        "gender$i" => $request->gender[$i],
        "email$i" => $request->email[$i],
        "contact$i" => $request->contact[$i],
        "address1$i" => $request->address1[$i],            
        if (!isEmpty($request->m_name)) {
            "address2$i" => $request->address2[$i],
        }  
        "town$i" => $request->town[$i],
        "mailbox$i" => $request->mailbox[$i],
        "insurance$i" => $request->insurance[$i],
    }

    Mail::send('emails.contact', $data, function($message) use ($data){
    $message->from(''sender@demo.com);
    $message->to('receiver@demo.com');
    $message->subject("!!New Mail!!");
      });

    );
  return view('success');
}

我尝试使用for循环将所有传入的请求保存在数组中,以便将其作为电子邮件发送。但是我得到了错误 Parse error: syntax error, unexpected 'for' (T_FOR), expecting ')'。什么是解决这个问题的最佳方法。

1 个答案:

答案 0 :(得分:0)

您可以使用$key =&gt; $value对创建这样的数组:

$data = array();

foreach($request->title as $key=>$value){

    $insert = array();

    $insert['title'.$key] = $value;
    $insert['f_name'.$key] = $request->f_name[$key];

    if (!isEmpty($request->m_name)) {
        $insert['m_name'.$key] = $request->m_name[$key];
    }            

    $insert['l_name'.$key] = $request->l_name[$key];
    $insert['dob_month'.$key] = $request->dob_month[$key];
    $insert['dob_day'.$key] = $request->dob_day[$key];
    $insert['dob_year'.$key] = $request->dob_year[$key];
    $insert['gender'.$key] = $request->gender[$key];
    $insert['email'.$key] = $request->email[$key];
    $insert['contact'.$key] = $request->contact[$key];
    $insert['address1'.$key] = $request->address1[$key];

    if (!isEmpty($request->m_name)) {
        $insert['address2'.$key] = $request->address2[$key];
    }  

    $insert['town'.$key] = $request->town[$key];
    $insert['mailbox'.$key] = $request->mailbox[$key];
    $insert['insurance'.$key] = $request->insurance[$key];

    array_push($data, $insert);
}

现在,发送此$data以及邮件。