2014-05-22 23 views
0

我有一個從用戶收集一些信息的表單中的清單。我試圖獲取用戶選擇的那些複選框的值。Laravel每次都返回所有複選框的值

,路線如下

Route::get('addoption', array('as' => 'getaddoption', 'uses' => '[email protected]')); 
Route::post('addoption', array('as' => 'postaddoption', 'uses' => '[email protected]')); 

這裏的控制器部

public function getCategoryForm(){ 

    //$this->option is a model interface which deals with table named 'options' having one column 'option' 

    $existing_options = $this->option->lists('option'); 
    return View::make('dashboard.addcategoryform')->with('existing_options', $existing_options); 
} 

這裏是一部分(在dashboard.addcategory視圖)形式與複選框

@foreach($existing_options as $existing_option) 
    {{Form::label($existing_option, $existing_option)}} 
    {{Form::checkbox($existing_option, $existing_option)}} 
@endforeach 

所以涉及的複選框已創建。現在我想知道用戶選擇的複選框的值。

我通過postAddOption方法

if($validator->passes()){ 
    $existing_option = $this->option->lists('option');   

    foreach($existing_option as $existing_opt){ 
      if(Input::get($existing_opt) == true){ 
       $selected_option[] = $existing_opt; 
     } 
    } 

    print_r($selected_option); 
} 

處理這種形式,但它給了我所有的複選框的數組。

回答

0

您所有的複選框應該有相同的名稱並附加[]

@foreach($existing_options as $existing_option) 
    {{ Form::checkbox('options[]', $existing_option) }} 
@endforeach 

現在,當你提交,將提交作爲將填充相應的選中的複選框的值的每一個值,所以你不會通過他們需要循環的數組。

if($validator->passes()){ 
    dd(Input::get('options')); 
}