How to add a new value using update method

Viewed 30

I am trying to add a new timeout value and retain the old timeout value using an update method or any similar method which I don't know which I like to ask of you. As shown in the first image below timein is 01:00:11 and timeout is 01:00:13 which I want to retain. However if that same person is signing out the first timeout which is 01:00:13 becomes 01:18:18 shown in the second image

The part of the code which I am referring to is shown below. Somehow I could not find a clue how to improve it. I also replace update with insert, CreateorUpdate but not working

if($attendance)
 {
                    
     DB::table('attendances')
                    ->where(['created_by' => $id, 'logdate' => $date] )
                    ->update([
                        'timeout' => $time,
                        'status'=> '1',
                        
                    ]);
                  
  }

Timein and Timeout A

Timein and Timeout B

users table

 Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('created_by')->nullable();
        $table->string('employee_id')->nullable();
        $table->string('name');
        $table->string('email', 100)->unique()->nullable();
        $table->string('password');
        $table->string('P')->default('0')
        $table->rememberToken();
        $table->timestamps();
    });

attendances table

Schema::create('attendances', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('created_by');
        $table->integer('user_id')->unsigned();
        $table->time('timein')->nullable();
        $table->time('timeout')->nullable();
        $table->date('logdate');
        $table->string('status');   
        $table->timestamps();
    });

timeclock methods

public function timeclock(Request $request){

            $id = $request->input('id');
            // dd($id);
            $date = carbon::today() ; 
            $time = Carbon::now(); 
        
            $user = DB::table("users")
                    ->where("id", "=", $id)
                    ->first();
        
            
            if($user == NULL) {
                $msg = "<div class='alert alert-danger'><button class='close' data-dismiss='alert'>&times;</button> Please enter your first name to continue!</div>";
              }
              elseif ($user != NULL){
                
                $attendance =   DB::table("users")
                ->leftJoin("attendances", function($join){
                    $join->on("users.id", "=", "attendances.user_id");
                })
                ->select("attendances.*", "logdate", "timein", "timeout", "status")
                ->where("attendances.user_id", "=", $id)
                ->where("attendances.logdate", "=", $date)
                ->where("attendances.status", "=", 0)
                ->get()->toArray();
                // dd($attendance);
                if($user->P == '1') {
        
                    DB::table('users')
                    ->where(['id' => $id] )
                    ->update([
                    'p'=> '0',
                        
                    ]);
        
        
                }
                
                // dd($attendance);
        
                if($attendance){
                    
     
                    DB::table('attendances')
                    ->where(['created_by' => $id, 'logdate' => $date] )
                    ->update([
                        'timeout' => $time,
                        'status'=> '1',
                        
                    ]);
                  
                }
             
            
              else {
        
                $result = new Attendance;
                        $result->user_id = $id;
                        $result->created_by = $id;
                        $result->timein = $time;
                        $result->logdate =  $date;
                        $result->status = 0;
                        $result->save();
                
                          }
        
                          if($user->P == '0') {
        
                            DB::table('users')
                            ->where(['id' => $id] )
                            ->update([
                            'p'=> '1',
                                
                            ]);     
                        }
            }
            
                return redirect()->route('attendance.timeclockcreate')->with('id', $id)->with('message', 'Inserted successfully.');
        }
   

form

<table class="table table-bordered table-striped">
                    <thead>
                        <tr>
                           <th> {{ __(' SL') }}</th>
                           {{--<th> {{ __(' EMP CD') }}</th>--}}
                           <th> {{ __(' EMPLOYEE NAME') }}</th>
                            <th> {{ __('TIMEMING') }}</th>
                            
                         
                            <!-- <th> {{ __(' Created At') }} </th> -->
                       
                           
                            
                        </tr>
                    </thead>
                    <tbody>

                        
                    @foreach($attendances as $key => $attd)
                        <tr>
                            <td>{{$key+1}}</td>
                            <td>{{ $attd['name'] }}</td>     
                            <td>
                            <form action="{{ route('attendance.timeclock') }}" method="post" name="department_add_form">
                            {{ csrf_field() }}
                            <input type="hidden" name="id" id="id" value="{{ $attd['id'] }}">
                        
                                @if($attd['P']=='1')
                                 <button type="submit" class="button-88, btn btn-warning" role="button">{{ __('SIGN OUT') }}</button>
                                @elseif($attd['P']=='0')
                                 <button type="submit" class="button-88, btn btn-primary" role="button">{{ __('SIGN  IN') }}</button>              
                                @endif
                       
                       
                            </form>
                            </td>   

                            
                        </tr>

                    @endforeach
                       
                    </tbody>
                </table>

 

Could you please help me in this

1 Answers

Solved: The ->whereNull('timeout') has saved the day like this

DB::table('attendances')
      ->where(['created_by' => $id, 'logdate' => $date] )
      ->whereNull('timeout')
      ->update(['timeout' => $time,'status'=> '1',]);
Related