【问题标题】:How to include multiple inputs in a foreach statement如何在 foreach 语句中包含多个输入
【发布时间】:2018-09-17 23:38:49
【问题描述】:

我有两个输入字段,用户可以重复这些输入字段以在单个表单提交中插入多条记录。我目前正在使用 foreach 循环遍历其中一个输入并获取值,但是现在我需要同时遍历两个输入以将数据保存在一起。

我想同时遍历input-typeinput-label 并将存储的值保存到数据库中。

我想我可以创建两个 foreach 循环,但这似乎是一种可怕的处理方式。

表格-

<form method="post" action="{{ route('savePage') }}">
    {{ csrf_field() }}
    <ul class="field-list">
        <li v-for="(input, index) in inputs">
            <input type="text" name="input-type[]" v-model="input.one" style="display: none">
            <input type="text" name="input-label[]" v-model="input.two" placeholder="Label name">
            <p><span class="codesnippet">text</span></p>
            <button class="p-btn secondary" @click.prevent="deleteRow(index)" >Remove field</button>
        </li>
    </ul>
    <button>Submit</button>
</form>

控制器 -

public function saveAtt(Request $request)
{
    foreach ($request->input('input-label') as $label) {

        $field = new Attribute;
        $field->page_id = 1;
        $field->label = $label;
        $field->type = 2;
        $field->save();
    }

    return redirect()->route('indexPage');
}

【问题讨论】:

    标签: php laravel laravel-5 vue.js


    【解决方案1】:

    使用这个:

    $types = $request->input('input-type');
    foreach ($request->input('input-label') as $i => $label) {
        $field = new Attribute;
        $field->page_id = 1;
        $field->label = $label;
        $field->type = $types[$i];
        $field->save();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-10
      • 1970-01-01
      • 2018-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多