【问题标题】:Laravel - Update only selected dataLaravel - 仅更新选定的数据
【发布时间】:2019-01-24 04:48:14
【问题描述】:

我有一张桌子,在桌子里面,我有文本框在这里查看页面。

现在的问题是每当我更新特定的行或 ID 时,它都会更新所有内容。如何在不更新所有内容的情况下只更新特定的行或 id?非常感谢

这是我的控制器

 public function updateSchedule(Request $request, $id)
{
    $timein = $request->input('timeIn');
    $timeout = $request->input('timeOut');

    DB::table('schedules')
        ->update(['time_in' => $timein, 'time_out' => $timeout]);

    $notification = array(
        'message' => 'Employee Time Updated!',
        'alert-type' => 'success'
    );
    return redirect()->back()->with($notification, 'Employee Time Updated!');

}

这是我的看法

    {!! Form::open(['action' => ['Admin\EmployeeFilemController@updateSchedule', $employee->id], '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>

        <table class="table">
            <thead>
              <tr>
                <th>DATE</th>
                <th>TIME IN</th>
                <th>LUNCH</th>
                <th>TIME OUT</th>
                <th>TOTAL HOURS</th>
                <th>STATUS</th>

              </tr>
            </thead>
            <tbody>

                    @foreach ($employeeSched as $setTime)
              <tr>

                 <td> {{Form::label('date_today', $setTime->date_today,['class'=>'form-control'])}}</td>
                 <td><input type="time" name="timeIn" class="form-control col-md-10" value='{{ $setTime->time_in }}'></td>
                 <td><p>12:00 PM - 1:00 PM</p></td>
                 <td><input type="time" name="timeOut" class="form-control col-md-10" value='{{ $setTime->time_out }}'></td>
                 <td>@php
                    $time1 = strtotime($setTime->time_in);
                    $time2 = strtotime($setTime->time_out);
                    $difference = round(abs($time2 - $time1) / 3600,2);
                    $total = $difference - 1;
                    echo '<b>' . $total . '</b>';
                 @endphp</td>


                <td>
                    @php
                    if ($total < 8) {
                        echo '<b>Late</b>';
                    } else if($total > 8) {
                        echo '<b>OT</b>';
                    } elseif ($total < 8.5 && $total == 8) {
                        echo '<b>In</b>';
                    }


                    @endphp
                </td>
              </tr>
              @endforeach



            </tbody>
    </table>
    {{Form::button('<i class="fa fa-clock">&nbsp;&nbsp;UPDATE TIME</i>',['type' => 'submit','class' => 'btn btn-info btn-sm',  'style'=>"display: inline-block;"])}}
    {{Form::hidden('_method', 'PUT')}}

    {!! Form::close() !!}

谢谢你帮帮我,谢谢

【问题讨论】:

    标签: php sql laravel laravel-5 eloquent


    【解决方案1】:

    您需要通过计划 ID 更新员工的计划时间,因为您必须更改多个计划 timeIn 和 timeOut。如下更新您的代码。我希望所提供的更改是不言自明的。

    刀片(仅需要的部分)

    <td><input type="time" name="schedules[{{ $setTime->id }}][timeIn]" class="form-control col-md-10" value='{{ $setTime->time_in }}'></td>
    <td><input type="time" name="schedules[{{ $setTime->id }}][timeOut]" class="form-control col-md-10" value='{{ $setTime->time_out }}'></td>
    

    注意:我假设,$setTime-&gt;id 是计划 ID

    控制器

    public function updateSchedule(Request $request, $id)
    {
        $schedules = $request->get('schedules');
    
        foreach ($schedules as $id => $schedule) {
            DB::table('schedules')
                ->where('id', $id)
                ->update([
                    'time_in'  => $schedule['timeIn'], 
                    'time_out' => $schedule['timeOut']
                ]);
        }
    
        $notification = array(
            'message' => 'Employee Time Updated!',
            'alert-type' => 'success'
        );
    
        return redirect()->back()->with($notification, 'Employee Time Updated!');
    }
    

    注意:即使没有修改所有记录,此实现也会触及 DB n(计划记录数)时间。

    提示:您可以将旧值与可修改的值一起传递,以比较它们是否实际被修改,并仅对已修改的值执行更新查询。

    【讨论】:

    • 这段代码非常合乎逻辑,谢谢先生,但还是不行:(
    • 当我将 $id 更改为 2 时,它正在更新,但是当我将其更改为 $id 时,它不会更新 :( 帮助先生,谢谢
    • dd($schedules) 的输出是什么?
    • 你好先生这里是dd($schedules)数组的结果:1 [▼ "$setTime->id" => 数组:2 [▶]]
    • 对不起,我的错,更新了答案。
    【解决方案2】:

    您需要在查询中添加 $id,这就是它更新整行的原因:

    DB::table('schedules')->where('id', $id)->update(['time_in' => $timein, 'time_out' => $timeout]);
    

    另外,我建议使用 Eloquent 代替查询生成器。

    【讨论】:

    • 这里DB::table('schedules')-&gt;where('id', $id) -&gt;update(['time_in' =&gt; $timein, 'time_out' =&gt; $timeout]);和你的一样,先生
    • 你能在哪里转储并显示你得到的东西,这个 id 是自动递增的吗?
    • 是的,先生,它是自动递增的,完全没有错误。但问题是数据根本没有更新。但它说它是成功的,但它没有更新
    • 是的,在你的情况下它总是会成功,因为你没有检查你的查询结果和简单的重定向成功消息。
    【解决方案3】:

    你也可以这样试试。

    DB::update('update schedules set time_in = "'.$timein.'", time_out = "'.$timeout.'" where id = "'.$id.'");
    

    【讨论】:

      【解决方案4】:

      您没有告诉数据库您要更新哪一行。当你写你的查询是“更新'表'设置'col_name'='值'”;但是您需要告诉它您要更新哪个 id。就像'其中'id' = 12'。 希望你能理解。 :)

       public function updateSchedule(Request $request, $id){
        $timein = $request->input('timeIn');
        $timeout = $request->input('timeOut');
        DB::table('schedules')->where('id', $id)->update(['time_in' => $timein, 'time_out'=> $timeout]);
        $notification = array(
          'message' => 'Employee Time Updated!',
          'alert-type' => 'success'
      );
      return redirect()->back()->with($notification, 'Employee Time Updated!');
      }
      

      【讨论】:

      • 是的,你是对的。我的意思是没有我想更新的特定 id 我只想在更改输入文本值时更新 id,现在代码不起作用先生 :(
      • 我仍然不清楚您是否说“我只是想在更改输入文本值时更新 id”。据我了解,我认为 Id 主键对吗?如果您在控制器中使用 dd($id) 会得到什么?
      【解决方案5】:

      您可以使用 where 子句按 id 选择,例如。

        DB::table('schedules')->where('id', $id)
              ->update(['time_in' => $timein, 'time_out' => $timeout]);
      

      此外,如果一切正常,您可以使用尝试事务或在出现问题时捕获错误。

      DB::beginTransaction();
      try {
        DB::table('schedules')->where('id', $id)
              ->update(['time_in' => $timein, 'time_out' => $timeout]);
      
          DB::commit();
      
      } catch (\Exception $e) {
          DB::rollback();
          print_r($e);die;
      
      }
      

      【讨论】:

        猜你喜欢
        • 2022-01-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-16
        • 2018-07-07
        相关资源
        最近更新 更多