2017-07-04 77 views
0

我有一個方法來處理來自動態表單的請求。用循環laravel保存到數組

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(''[email protected]); 
    $message->to('[email protected]'); 
    $message->subject("!!New Mail!!"); 
     }); 

    ); 
    return view('success'); 
} 

我試圖保存所有傳入的請求在數組中使用for循環爲了發送它作爲電子郵件。但我得到的錯誤是 Parse error: syntax error, unexpected 'for' (T_FOR), expecting ')'。這個問題最好的辦法是什麼?

+0

您無法在Array內運行循環。這就是它給出錯誤的原因。嘗試在數組外使用for循環來向數組填充數組@@ tanja-forsberg –

+0

您應該在數組外循環,並追加到for循環中的數組 – linktoahref

+0

請檢查 - > $ message-> from('sender @ demo.com); –

回答

0

您可以使用$key =>$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