使用具有相同名称的值数组提交表单

时间:2015-08-02 22:04:53

标签: php laravel laravel-5

我在Laravel中创建配方应用程序,每个配方都会有不确定的成分。用户将能够单击按钮以动态添加额外的数量,度量和成分字段。现在我只有3个字段用于测试。成分和度量选择框正在从其自己的数据库表中填充,其ID为值。这是我到目前为止所做的。

查看表格

              <form action="store">
                    <input type="hidden" name="_token" value="{{ csrf_token() }}">
                    <div class="row">
                        <button class="btn btn-default" type="submit">Add Recipe</button>
                        <div class="col-xs-6">
                            <label for="recipe_name">Recipe Name</label>
                            <input type="text" name="recipe_name" class="form-control input-sm" id="recipe_name">
                        </div>
                        <br />
                        <div class="col-xs-8">
                            <label for="recipe_desc">Recipe Description</label>
                            <input type="text" name="recipe_desc" class="form-control input-sm" id="recipe_desc">
                        </div>
                        <br />
                    </div>
                    <br />
                    <div class="row">
                      <div class="col-xs-2">
                        <label for="amount">Amount</label>
                        <input type="number" name="amount[]" class="form-control input-sm" id="amount" />
                      </div>
                      <div class="col-xs-2">
                        <label for="measure">Measure</label>
                        {!! Form::select('measure[]', 
                            (['0' => 'Measure'] + $measure), null, 
                            ['class' => 'form-control input-sm', 'id' => 'measure']) !!}
                      </div>
                      <div class="col-xs-4">
                        <label for="ingredient">Ingredient</label>
                        {!! Form::select('ingredient[]', 
                            (['0' => 'Select Ingredient'] + $ingredients), null, 
                            ['class' => 'form-control input-sm', 'id' => 'ingredient']) !!}
                      </div>
                    </div>
                    <br>
                    <div class="row">
                      <div class="col-xs-2">
                        <label for="amount">Amount</label>
                        <input type="number" name="amount[]" class="form-control input-sm" id="amount" />
                      </div>
                      <div class="col-xs-2">
                        <label for="measure">Measure</label>
                        {!! Form::select('measure[]', 
                            (['0' => 'Measure'] + $measure), null, 
                            ['class' => 'form-control input-sm', 'id' => 'measure']) !!}
                      </div>
                      <div class="col-xs-4">
                        <label for="ingredient">Ingredient</label>
                        {!! Form::select('ingredient[]', 
                            (['0' => 'Select Ingredient'] + $ingredients), null, 
                            ['class' => 'form-control input-sm', 'id' => 'ingredient']) !!}
                      </div>
                    </div>
                    <br />
                    <div class="row">
                      <div class="col-xs-2">
                        <label for="amount">Amount</label>
                        <input type="number" name="amount[]" class="form-control input-sm" id="amount" />
                      </div>
                      <div class="col-xs-2">
                        <label for="measure">Measure</label>
                        {!! Form::select('measure[]', 
                            (['0' => 'Measure'] + $measure), null, 
                            ['class' => 'form-control input-sm', 'id' => 'measure']) !!}
                      </div>
                      <div class="col-xs-4">
                        <label for="ingredient">Ingredient</label>
                        {!! Form::select('ingredient[]', 
                            (['0' => 'Select Ingredient'] + $ingredients), null, 
                            ['class' => 'form-control input-sm', 'id' => 'ingredient']) !!}
                      </div>
                    </div>
                    <br />
                    <div class="row">
                        <div class="col-xs-8">
                            <label for="recipe_instruct">Recipe Description</label>
                            <textarea rows="3" name="recipe_instruct" class="form-control input-sm" id="recipe_instruct"></textarea>
                        </div>
                    </div>
                </form>

RecipeController

public function store(Request $request)
{
    $recipeName = $request->input('recipe_name');
    $recipeDesc = $request->input('recipe_desc');
    $recipeInstruct = $request->input('recipe_instruct');
    $ingredients = $request->input('ingredient');

    $id = DB::table('recipe')->insertGetId([
        'name'          => $recipeName,
        'description'   => $recipeDesc,
        'instructions'  => $recipeInstruct
        ]);

    foreach($ingredients as $ingredient)

    DB::table('recipe_ingredient')->insert([??????]);
}

如果我做了一个dd,它输出的是正确的

    array:7 [▼
  "_token" => "DmAHXPc2VxibzbJTDlmSRxcS6XZPTq906IKbz6Hf"
  "recipe_name" => "Test Recipe"
  "recipe_desc" => "Test Recipe Description"
  "amount" => array:3 [▼
    0 => "1"
    1 => "2"
    2 => "3"
  ]
  "measure" => array:3 [▼
    0 => "8"
    1 => "9"
    2 => "8"
  ]
  "ingredient" => array:3 [▼
    0 => "4"
    1 => "3"
    2 => "7"
  ]
  "recipe_instruct" => "Test recipe instructions"
]

我在配方数据库中插入recipe_name,recipe_desc和recipe_instruct并返回id。然后我使用该id来引用recipe_ingredient表中的成分id,度量id和amount id。

我是否需要遍历每个数组以将它们分别插入recipe_ingredient表中?或者是否有一种更容易,更有说服力的方式一次性完成这项工作?

RecipeIngredient表

 Schema::create('recipe_ingredient', function (Blueprint $table) {
        $table->integer('recipe_id')->unsigned();
        $table->integer('ingredient_id')->unsigned();
        $table->integer('measure_id')->unsigned();
        $table->integer('amount');

        $table->foreign('recipe_id')->references('id')->on('recipe')->onDelete('cascade');
        $table->foreign('ingredient_id')->references('id')->on('ingredient')->onDelete('cascade');
        $table->foreign('measure_id')->references('id')->on('measure')->onDelete('cascade');
        $table->timestamps();
    });

食谱模型

 public function ingredients()
{
    return $this->hasMany('App\RecipeIngredient');
}

RecipeIngredient模型

 public function recipe()
{
    return $this->belongsTo('App\Recipe');
}

1 个答案:

答案 0 :(得分:0)

我想出了一个解决方案。也许不是正确的方法,但它有效。

<强> RecipeController

 public function store(Request $request)
{
    $recipeName = $request->input('recipe_name');
    $recipeDesc = $request->input('recipe_desc');
    $recipeInstruct = $request->input('recipe_instruct');
    $ingredients = $request->input('ingredient');
    $measure = $request->input('measure');
    $amount = $request->input('amount');

    $id = DB::table('recipe')->insertGetId([
        'name'          => $recipeName,
        'description'   => $recipeDesc,
        'instructions'  => $recipeInstruct
        ]);

    $i = 0;

  foreach($ingredients as $ingredient) 
  {

    DB::table('recipe_ingredient')->insert(
        ['recipe_id' => $id, 'ingredient_id' => $ingredients[$i], 'measure_id' => $measure[$i], 'amount' =>$amount[$i]]
    );

    $i++;
  }

  return "Success!";