laravel在多行和一个循环中插入数据

时间:2016-01-21 13:57:33

标签: php laravel blade

我在laravel中遇到一个问题,即在多行中插入数据。我设法在成分表中插入名称,但我想添加2个其他输入(单位和数量)。

我可以不创建两个循环吗?

控制器:

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--;
})

1 个答案:

答案 0 :(得分: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]' />