user10701881
user10701881

Reputation:

Laravel - Update only selected data

I have a table and inside the table, I have textbox here take a look for the Page.

enter image description here

now the problem is whenever I update specific row or id, then it updates everything. how can I just update the specific row or id without updating everything? thank you very much

here is my controller

 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!');

}

and here is my view

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

thank you kindly help me please thanks

Upvotes: 0

Views: 7923

Answers (5)

Ijas Ameenudeen
Ijas Ameenudeen

Reputation: 9259

You need to update the Scheduled times of an Employee by the Schedule ID since you have to change multiple schedule timeIn & timeOut. Update your code as below. I hope the changes provided are self-explanatory.

Blade (Only the required part)

<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>

Note: I assumed, $setTime->id is the Schedule ID

Controller

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!');
}

Note: This implementation will touch the DB n(number of schedule records) time even though all the records aren't modified.

Tip: You can pass the old values along with the modifiable ones to compare if they are actually modified and execute the update query only for those are modified.

Upvotes: 2

r&#252;ff0
r&#252;ff0

Reputation: 943

you can select by id using where clause, ex.

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

also, you can use try a transaction, if everything is ok or catch the error if something is wrong.

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;

}

Upvotes: 0

sabbir chowdury
sabbir chowdury

Reputation: 158

You did not tell your db which row you want to update . As you write your query is that " update 'table' set 'col_name' = 'value' "; But you need to tell it which id you want to update. Like 'where 'id' = 12 '. Hope you understand. :)

 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!');
}

Upvotes: 0

vinod gami
vinod gami

Reputation: 67

You can also try in this way.

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

Upvotes: 0

Ankur Tiwari
Ankur Tiwari

Reputation: 2782

You need to add $id inside your query, This is the reason it updates your whole row:

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

Also, In place of query builder, I'll suggest using Eloquent.

Upvotes: 0

Related Questions