2016-01-21 82 views
0

我在laravel中插入多行數據時遇到問題。我設法在配料表中插入名稱,但我想添加2個其他輸入(單位和數量)。laravel將數據插入多行並在一個循環中

我可以在不創建兩個循環的情況下做到這一點嗎?

控制器:

public function store() { 

    $meal = new Meal; 
    $meal->name = Input::get('name'); 
    $ingredient_names = Input::get('ingredient_names'); 
    $meal_ingredients = array(); 
    foreach($ingredient_names as $ingredient_name) 
    { 
     $meal_ingredients[] = new Ingredient(array(
      'name'=>$ingredient_name 
     )); 
    } 

    //save into the DB 
    $meal->save(); 
    $meal->ingredients()->saveMany($meal_ingredients); 
} 

create.blade.html:

<div class="panel-body"> 
     {{ Form::open(array('route' => 'meals.store')) }} 
      <div class="form-group"> 
       {{ Form::label('name', 'Name') }} 
       {{ Form::text('name', null, array('class' => 'form-control')) }} 
      </div> 
      <div class="input_fields_wrap"> 
       <input class="form-control" type="text" name="ingredient_names[]"> 
       <a class="add_field_button">Ajouter</a> 
      </div> 


      {{ Form::submit('Valider', array('class' => 'btn btn-primary')) }} 
     {!! Form::close() !!} 
    </div> 

meal.js(只是告訴你,我正在使用動態字段)

// dynamic fields 
var max_fields  = 20; //maximum input boxes allowed 
var wrapper   = $(".input_fields_wrap"); //Fields wrapper 
var add_button  = $(".add_field_button"); //Add button ID 

var x = 1; //initial text box count 
$(add_button).click(function(e){ //on add input button click 
    e.preventDefault(); 
    if(x < max_fields){ //max input box allowed 
     x++; //text box increment 
     $(wrapper).append('<div><input class="form-control" type="text" name="ingredient_names[]"/><a href="#" class="remove_field">Supprimer</a></div>'); //add input box 
    } 
}); 

$(wrapper).on("click",".remove_field", function(e){ //user click on remove text 
    e.preventDefault(); $(this).parent('div').remove(); x--; 
}) 
+0

數量和單位屬於原料嗎?爲什麼你需要做其他的循環,如果字段數是相同的,所以你可以簡單地填充單位和數量在同一個循環?! – Iamzozo

+0

是數量和單位屬於配料。那麼我將不得不做另外兩個foreach循環,比如foreach for the component_names? – Snake

回答

1

輸入字段可以是數組。我建議,對於每種成分,你有這樣的輸入:

<div class="row"> 
    <div class="col-sm-4"> 
     <div class="form-group"> 
      {{ Form::label('name', 'Name') }} 
      {{ Form::text('ingredient[0][name]', null, array('class' => 'form-control')) }} 
     </div> 
    </div> 
    <div class="col-sm-4"> 
     <div class="form-group"> 
      {{ Form::label('unit', 'Unit') }} 
      {{ Form::text('ingredient[0][unit]', null, array('class' => 'form-control')) }} 
     </div> 
    </div> 
    <div class="col-sm-4"> 
     <div class="form-group"> 
      {{ Form::label('quantity', 'Quantity') }} 
      {{ Form::text('ingredient[0][quantity]', null, array('class' => 'form-control')) }} 
     </div> 
    </div> 
</div> 

之後,當添加一行,遞增成分指數,這樣的下一行的輸入是像這樣:ingredient[1][name]等,然後你的店方法是這樣的:

public function store() { 
    $meal = new Meal; 
    $meal->name = Input::get('name'); 

    $ingredients = Input::get('ingredient'); 
    $meal_ingredients = array(); 

    foreach($ingredients as $ingredient) 
    { 
     $meal_ingredients[] = new Ingredient(array(
      'name' => $ingredient['name'], 
      'unit' => $ingredient['unit'], 
      'quantity' => $ingredient['quantity'], 
     )); 
    } 

    //save into the DB 
    $meal->save(); 
    $meal->ingredients()->saveMany($meal_ingredients); 
} 

我不是太熟悉Form類,所以,語法,讓您的輸入爲陣列可能會略有不同,但本質上,你想用這個來結束:

<input type='text' name='ingredient[0][name]' /> 
+0

很好,謝謝! – Snake