donglu7816 2016-01-21 13:57
浏览 176
已采纳

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

I have an issue in laravel with inserting data in multiple rows. I managed to insert the names in the ingredient table but I would like to add 2 other inputs (unit and quantity).

Can I do this without creating two more loops ?

Controller:

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 (just to show you that I am using dynamic fields)

// 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条回答 默认 最新

  • dou72260 2016-01-21 15:23
    关注

    Input fields can be arrays. I would recommend that for each ingredient, you have inputs like this:

    <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>
    

    Then, whenever you add a row, increment the ingredient index so that the next row's inputs are like so: ingredient[1][name], etc. Then your store method would look like this:

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

    I am not too familiar with the Form class, so the syntax to get your inputs to be arrays might be slightly different, but essentially, you want to end up with this:

    <input type='text' name='ingredient[0][name]' />
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 FIR滤波器算法设计与实现(利用库函数firdec()
  • ¥15 利用单片机产生正弦信号,来测量差分放大电路放大倍数和共模放大倍数的思路和仿真
  • ¥60 关于mavlink的论文咨询
  • ¥66 定制开发肯德基自动化网站下单软件
  • ¥15 中科院研发的TextMind(文心)
  • ¥20 vscode虚拟环境依赖包未安装
  • ¥15 odoo17关于owl开发js代码问题
  • ¥15 光纤中多普勒频移公式的推导
  • ¥15 怎么制作一个人脸识别门禁系统
  • ¥20 大华dss监控平台网络关闭登不进去