【问题标题】:Laravel Inserting Data Only the Last List or DataLaravel 仅在最后一个列表或数据中插入数据
【发布时间】:2019-06-22 04:14:54
【问题描述】:

我要插入数据具体看我的页面。

当我设置时间时,数据将被插入。但是我现在拥有的是插入的数据是该表上的最后一个数据January-31-2019 我该如何修复它所以当我插入数据时,数据将插入表上列出的任何数据.

控制器

public function insertSchedule(Request $request)
{
    $employeeTimeSet = new Schedule;
    $employeeTimeSet->employee_no = $request->input('hidEmployeeno');
    $employeeTimeSet->last_name = $request->input('hidEmployeeLast');
    $employeeTimeSet->first_name = $request->input('hidEmployeeFirst');
    $employeeTimeSet->date_today = $request->input('dateToday');
    $employeeTimeSet->time_in = $request->input('timeIn');
    $employeeTimeSet->time_out = $request->input('timeOut');
    $employeeTimeSet->save();

    $notification = array(
        'message' => 'Click PLAN TIME! Employee Time Set!',
        'alert-type' => 'success'
    );

    return redirect()->back()->with($notification, 'Employee Time Set');
}

查看

{!! Form::open(['action' => 'Admin\EmployeeFilemController@insertSchedule', 'method' => 'POST']) !!}
<div class="row">
    <div class="form-group col-md-12">
        <small>Employee No. and Name:</small>
        <b><i> {{ $employee->employee_no }} : {{ $employee->last_name }}, {{ $employee->first_name }}</i></b>
        <input type="hidden" name="hidEmployeeno" value='<?php echo $employee->employee_no ?>'>
        <input type="hidden" name="hidEmployeeLast" value='<?php echo $employee->last_name ?>'>
        <input type="hidden" name="hidEmployeeFirst" value='<?php echo $employee->first_name ?>'>
        <hr>
    </div>
</div>
@php
    $today = today(); 
    $dates = []; 

    for($i=1; $i < $today->daysInMonth + 1; ++$i) {
        $dates[] = \Carbon\Carbon::createFromDate($today->year, $today->month, $i)->format('F-d-Y');
    }
@endphp

<table class="table">
    <thead>
    <tr>
        <th>DATE TODAY</th>
        <th>TIME IN</th>
        <th>TIME OUT</th>
        <th>ACTION</th>
    </tr>
    </thead>
    <tbody>
    @foreach($dates as $date)
        <tr>
            <td><b>{{ $date }}</b></td>
            <input type="hidden" name="dateToday" value="{{ $date }}">
            <td><input type="time" name="timeIn" class="form-control col-md-10"></td>
            <td><input type="time" name="timeOut" class="form-control col-md-10"></td>
            <td> {{Form::button('<i class="fa fa-clock">&nbsp;&nbsp;SET TIME</i>',['type' => 'submit','class' => 'btn btn-warning btn-sm',  'style'=>"display: inline-block;"])}}</td>
        </tr>
    @endforeach
    </tbody>
</table>
{!! Form::close() !!}

【问题讨论】:

标签: php arrays laravel laravel-5 eloquent


【解决方案1】:

您需要使用 [] 作为您的文本框名称。示例:name[],您需要使用循环来发布它。示例 - how to get post from same name textboxes in php

【讨论】:

    【解决方案2】:

    问题是您正在使用一种包含多个输入的表单,并且它们没有唯一的名称。如果您检查代码,您的表单将有 31 个名称为 timeIn 的输入和 31 个名称为 timeOut 的输入。

    当你提交表单时,它会选择最后一个 timeIntimeOut

    两种解决方案

    1. 制作多个表单(将表单标签放在while循环中,会在页面中创建31个表单)
    2. 使用 ajax 向服务器发送数据。

    【讨论】:

      【解决方案3】:

      除了@Swaroop 的答案之外,还有另一种选择。您可以在同一表单中制作这些字段的数组并提交它们。在服务器端,您将不得不遍历发布的数组,以便您可以进一步处理它们。

      【讨论】:

        猜你喜欢
        • 2019-06-22
        • 1970-01-01
        • 2020-10-21
        • 1970-01-01
        • 2021-09-03
        • 2020-05-30
        • 2021-10-27
        • 1970-01-01
        • 2018-05-28
        相关资源
        最近更新 更多