PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, February 12, 2022

[FIXED] Update Laravel column based on another column from the same query

 February 12, 2022     laravel     No comments   

Issue

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


Solution

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


Answered By - Aria Shahdadi
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

1,259,002

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © 2025 PHPFixing