Beusebiu
Beusebiu

Reputation: 1513

Update Laravel column based on another column from the same query

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

Upvotes: 0

Views: 1505

Answers (1)

Aria Shahdadi
Aria Shahdadi

Reputation: 301

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

Upvotes: 1

Related Questions