dongxi8993 2019-07-18 04:32
浏览 46
已采纳

存储来自多个输入字段的数组值 - Laravel

What I'm trying to do is Schedule form represent in table with input field like day, start time, and end time.

In blade file, I have an array that I assigned values which is represent days (7 in total). The reason I put in array cause I don't want to write html code 7 times. I do FOR loop to display input field with assigned value.

Then, I want to input another value which is 'start time' and 'end time' according to each day.

Below is my code

In blade


    <?php
          $days = array("Monday","Tuesday","Wednesday","Thursday","Friday","Satuday");
          $daylength=count($days);

          for($i=0; $i<$daylength; $i++)
          {
     ?>
            <tr>
            <td><input type="text" name="day[]" class="form-control" value="{{ $days[$i] }}" disabled/></td>
            <td><input type="time" name="start_time[]" class="form-control"/></td>
            <td><input type="time" name="end_time[]" class="form-control"/></td>
            <td></td>
            </tr>

    <?php } ?>

In controller

$days = $request->input('day');
$start_time = $request->input('start_time');
$end_time = $request->input('end_time');
$zone_id = $zone->id;

    for($count = 0; $count < count($days); $count++)
    {
        $data = array(
                'day' => $days[$count],
                'start_time'  => $start_time[$count],
                'end_time'  => $end_time[$count],
                'zone_id'  => $zone_id,
              );

        $insert_schedule[] = $data; 
     }
     Schedule::insert($insert_schedule);

What I expected, it should store input values into database table. The problem is after submitting the form, controller request empty value.

The error shown :

count(): Parameter must be an array or an object that implements Countable

Hope anyone can help. Thank you in advanced.

  • 写回答

4条回答 默认 最新

  • dongqigu0429 2019-07-18 04:46
    关注

    disabled input won't submit. You need to use readonly if you want to make it not editable by user.

    <input type="text" name="day[]" class="form-control" value="{{ $days[$i] }}" readonly/>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀