2

I'm trying to update the hours_worked column in daiy_attendance table using in_time and out_time in same table. in_time and out_time already stored in the table.

So I'm using timediff to get the difference. Query runs and execute correctly when I try it on the phpmyadmin sql format, but when I try to run it in the program it does not update the hours_worked column. It does not gives any errors.

$sql5 = "UPDATE daily_attendances SET hours_worked = TIMEDIFF(out_time,in_time) WHERE in_time != '' AND out_time != '' ";
$result2 = DB::statement(DB::raw($sql5));
5
  • Not sure if it's relevant: did you set 'time_worked' as fillable? Commented Aug 1, 2017 at 3:54
  • @JohnDoe it is nullable Commented Aug 1, 2017 at 3:56
  • 1
    @JohnDoe OP is not using a model, so $fillable attributes are irrelevant. Commented Aug 1, 2017 at 4:33
  • @JohnDoe yes i have added Commented Aug 1, 2017 at 4:34
  • put your query in try.. catch() block and see if there is any error.. Commented Aug 1, 2017 at 5:48

1 Answer 1

4

try

DB::table('daily_attendances')
                ->whereRaw("in_time != '' AND out_time != ''")
                ->update(['hours_worked ' =>DB::raw('TIMEDIFF(out_time,in_time)')]);

OR JUST use db:statement

$sql5 = "UPDATE daily_attendances SET hours_worked = TIMEDIFF(out_time,in_time) WHERE in_time != '' AND out_time != '' ";
$result2 = DB::statement($sql5);
Sign up to request clarification or add additional context in comments.

2 Comments

Check your database connection , is it connecting to same database that you tried from phpmyadmin
connection is fine

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.