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 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)