【问题标题】:Forloop through input's and retrieve the valueFor 循环输入并检索值
【发布时间】:2018-02-08 16:03:43
【问题描述】:

我有这个问题:

当我循环遍历 50 个输入并且我只填写了第一个输入时,请求/发布返回 null。但是当我填写最后一个输入时,它会返回值。

控制器代码

public function store(Request $request)
{
    $data = $request->get('type');

    $country = Country::create([
            'user_id' => '1',
            'country' => $data['name']
    ]);
}

这是表格

<div class="container">
    <form action="/form" method="post">
        {{csrf_field()}}
        @for($x = 0; $x++ < 50;)
            <input type="text" name="type[name]">
        @endfor

        <button type="submit">Submit</button>
    </form>
</div>

【问题讨论】:

  • 你需要数组输入,在你的控制器中将这个&lt;input type="text" name="type[name]"&gt;更改为这个&lt;input type="text" name="type[name][]"&gt;,你必须通过将到达的输入数组插入到你的模型中
  • 嗯,可以,但我的情况是我有 2 个输入,type[id] 和 type[name]。我如何在控制器中捕捉到它?
  • 那么你有 50 个输入还是两个输入?
  • 50 个输入,50 个 id 输入和 50 个 name 输入
  • 问题出在哪里?

标签: php html arrays forms laravel


【解决方案1】:

您输入的名称不正确。如果你想拥有多个同名的输入,你已经将它声明为一个数组。

在 HTML 中替换

{{csrf_field()}}
@for($x = 0; $x++ < 50;)
    <input type="text" name="type[name]">
@endfor

{{csrf_field()}}
@for($x = 0; $x++ < 50;)
    <input type="text" name="name[]">
    <input type="text" name="id[]"> // since you said you have two inputs
@endfor

在你的控制器中

循环输入数组并添加代码以将其插入数据库。

public function store(Request $request)
{
    $names = $request->get('name');  // get posted name array
    $ids = $request->get('id');  // get posted id array
    if(!empty($id) && !empty($name)){  // validations
      foreach($names as $key=>$value){
         // you can add more validations here
         $country = Country::create([
            'user_id' => $ids[$key],
            'country' => $value
         ]);
        }
    }
}

【讨论】:

    【解决方案2】:

    很难理解您要达到的目标,但是:

    <form action="/form" method="post">
        {{csrf_field()}}
        @for($x = 0; $x++ < 50;)
            <input type="text" name="type[{{$x}}][id]">
            <input type="text" name="type[{{$x}}][name]">
        @endfor
    
        <button type="submit">Submit</button>
    </form>
    

    这是你的表格。在name 属性中显式 设置索引。在这种情况下,您的 $request-&gt;type 将如下所示:

    array(
        0 => array(id => value0, name => value0)
        1 => array(id => value1, name => value1)
        // more items
    )
    

    在控制器中,您可以对其进行迭代:

    foreach ($request->type as $value) {
        $country = Country::create([
            // don't know where you should put id, but
            'id_key' => $value['id'],
            'country' => $value['name'],
        ]);
    }
    

    【讨论】:

    • 是的,这项工作是很好的解决方案!现在我必须找出当 $value 为空时该怎么办。
    • 检查empty,如果name或id为空则不要插入。
    【解决方案3】:

    好的,所以我结合了@ravinder 和@u_mulder 的答案。这是对我有用的解决方案。

    形式:

    <form action="/form" method="post">
    {{csrf_field()}}
    @for($x = 0; $x++ < 50;)
        <input type="text" name="type[{{$x}}][id]">
        <input type="text" name="type[{{$x}}][name]">
    @endfor
    <button type="submit">Submit</button>
    

    控制器

        public function store(Request $request)
    {
    
        foreach ($request->type as $value) {
            if(!empty($value['id']) && !empty($value['name'])) { // if they are not empty proceed
                $country = Country::create([ // Create a country
                    'user_id' => $value['id'],
                    'country' => $value['name'],
                ]);
            }
        }
    }
    

    感谢大家的帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-14
      • 1970-01-01
      • 2017-05-27
      • 2019-01-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多