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

2021-12-26 00:00:00 arrays php laravel laravel-5 eloquent

我有这个月的天数列表.但问题是当我插入或添加数据时,它只输入了列表中的最后一个数组或数据.例如,我添加 Jan-02-2019, 然后它会插入 Jan-31-2019.

控制器

公共函数insertSchedule(Request $request){$employeeTimeSet = 新时间表;$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' => 'AdminEmployeeFilemController@insertSchedule', 'method' => 'POST']) !!}<div class="row"><div class="form-group col-md-12"><small>员工编号和姓名:</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 ?>'><小时>

@php$今天=今天();$dates = [];for($i=1; $i <$today->daysInMonth + 1; ++$i) {$dates[] = CarbonCarbon::createFromDate($today->year, $today->month, $i)->format('F-d-Y');}@endphp<table class="table"><头><tr><今天>今天</th><th>TIME IN</th><th>超时</th><th>Action</th></tr></thead>@foreach($dates 作为 $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>{!!表单::关闭() !!}

解决方案

我认为这是因为您构建 HTML 表单的方式.如果我正确理解了您的问题,以下内容应该可以解决问题:

@php$今天=今天();$dates = [];for($i=1; $i <$today->daysInMonth + 1; ++$i) {$dates[] = CarbonCarbon::createFromDate($today->year, $today->month, $i)->format('F-d-Y');}@endphp<div class="row"><div class="form-group col-md-12"><small>员工编号和姓名:</small><b><i>{{ $employee->employee_no }} : {{ $employee->last_name }}, {{ $employee->first_name }}</i></b><小时>

<table class="table"><头><tr><今天>今天</th><th>TIME IN</th><th>超时</th><th>Action</th></tr></thead>@foreach($dates 作为 $date){!!Form::open(['action' => 'AdminEmployeeFilemController@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>{!!表单::关闭() !!}@endforeach</tbody>

I have a list of days for this month. But the problem is when I insert or add the data it only entered the last array or data from the list. For example, I add Jan-02-2019, and it will then insert Jan-31-2019.

Controller

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();
}

Here are the view and form:

{!! Form::open(['action' => 'AdminEmployeeFilemController@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[] = CarbonCarbon::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() !!}

解决方案

I think it's because of the way you structured your HTML form. If I understood your question correctly, the following should make it work:

@php
    $today = today(); 
    $dates = []; 
    for($i=1; $i < $today->daysInMonth + 1; ++$i) {
        $dates[] = CarbonCarbon::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' => 'AdminEmployeeFilemController@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>

相关文章