数据仅在 Laravel 应用程序中插入最后一个数组

Posted

技术标签:

【中文标题】数据仅在 Laravel 应用程序中插入最后一个数组【英文标题】:Data inserts the last array only in a Laravel app 【发布时间】:2019-06-22 01:26:36 【问题描述】:

我有这个月的天数列表。但问题是当我插入或添加数据时,它只输入了最后一个数组或列表中的数据。例如,我添加Jan-02-2019,,然后它会插入Jan-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();

这里是视图和表单:

!! 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() !!

【问题讨论】:

@KarlHill:感谢您在这里编辑。一般而言,如果您遇到诸如“请帮助我”及其变体之类的细枝末节或礼貌,您可以将其删除。任何与问题无关的内容都可以删除。 【参考方案1】:

我认为这是因为您构建 html 表单的方式。如果我正确理解了您的问题,则以下内容应该可以正常工作:

@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
<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>
        <hr>
    </div>
</div>
<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)
        !! Form::open(['action' => 'Admin\EmployeeFilemController@insertSchedule', 'method' => 'POST']) !!
            <input type="hidden" name="hidEmployeeno" value=" $employee->employee_no ">
            <input type="hidden" name="hidEmployeeLast" value=" $employee->last_name ">
            <input type="hidden" name="hidEmployeeFirst" value=" $employee->first_name ">
            <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>
        !! Form::close() !!
    @endforeach
    </tbody>
</table>

【讨论】:

先生,您能解释一下为什么表格是问题吗?有一个数组对吗? 先生再次感谢您。最后一个问题是我无法插入或添加第一个数组或数据January-01-2019 请突出显示更改并进行解释,以便 OP 了解问题所在以及您如何解决问题

以上是关于数据仅在 Laravel 应用程序中插入最后一个数组的主要内容,如果未能解决你的问题,请参考以下文章

仅在循环中多次插入最后一个值

laravel 8:插入方法在 eloquent 中获取最后一个插入 id(插入方法)

仅在每次注销时更新time_out字段到数据库中用户的最后插入ID?

如何在 Eloquent ORM laravel 中获取最后一个插入 ID

java基础 insert方法问题?

使用 Laravel Eloquent 获取最后插入的 ID