Home > Back-end >  Update Laravel column based on another column from the same query
Update Laravel column based on another column from the same query

Time:01-25

I want to update all row that has no 'timemodified' based on another column from the same query.

DB::table('sco_tracks')->where('timemodified', '')->update([
            'timemodified' => Carbon::createFromFormat('Y-m-d H:i:s' , DB::raw('updated_at'))->timestamp
        ]);

With this code I get: Data missing {"exception":"[object] (InvalidArgumentException(code: 0): A four digit year could not be found

CodePudding user response:

the raw query would be

UPDATE sco_tracks t SET timemodified=UNIX_TIMESTAMP(created_at) WHERE timemodified IS NULL;

the code for laravel:

DB::table("sco_tracks")->where('timemodified','')->update(['timemodified'=>DB::raw('UNIX_TIMESTAMP(updated_at)')]);

and if the value of timemodified field is null you can use this code:

DB::table("sco_tracks")->whereNull('timemodified')->update(['timemodified'=>DB::raw('UNIX_TIMESTAMP(updated_at)')]);
  •  Tags:  
  • Related