【问题标题】:Laravel - Insert Array of data into databaseLaravel - 将数据数组插入数据库
【发布时间】:2019-01-29 11:47:18
【问题描述】:

您好,如何实现在数组中插入数据?使用雄辩?现在我有这个在我的

控制器

public function insertSchedule(Request $request)
{
    $employAdd = [];

    foreach($employAdd as $key){

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

【问题讨论】:

    标签: php laravel laravel-5 eloquent


    【解决方案1】:

    从我们的聊天中,我更新了答案。您的控制器中的以下内容应该可以工作:

    public function insertSchedule(Request $request)
    {
        Schedule::create([
            'employee_no' => $request->input('hidEmployeeno'),
            'last_name' => $request->input('hidEmployeeLast'),
            'first_name' => $request->input('hidEmployeeFirst'),
            'date_today' => $request->input('dateToday'),
            'time_in' => $request->input('timeIn'),
            'time_out' => $request->input('timeOut'),
        ]);
    }
    

    然后您需要将您的表单更新为如下所示,注意表单标签属于您要插入的每一行。

    <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>
    
    @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)
        {!! Form::open(['action' => 'Admin\EmployeeFilemController@insertSchedule', 'method' => 'POST']) !!}
        <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 ?>'>
        <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>
    

    【讨论】:

    • 先生,它有一个错误Invalid argument supplied for foreach()
    • ,请问先生是什么错误,我们该如何解决?非常感谢你hh
    • 我们正在循环遍历您视图中的值之一。 dd($data)的输出是什么
    • 这里是输出先生array:7 [▼ "_token" =&gt; "fMysI7wGe3rtC8cJJ8ADqemazyykFiOO7XXDLfy8" "hidEmployeeno" =&gt; "10310" "hidEmployeeLast" =&gt; "Rizal" "hidEmployeeFirst" =&gt; "Jose" "dateToday" =&gt; "January-31-2019" "timeIn" =&gt; null "timeOut" =&gt; null ]
    • 数组也得到了最后的数据先生,这是为什么呢:(
    猜你喜欢
    • 1970-01-01
    • 2015-12-03
    • 2020-07-05
    • 2021-10-27
    • 2018-06-16
    • 2020-12-31
    • 2021-09-10
    • 2015-02-22
    • 1970-01-01
    相关资源
    最近更新 更多