insert current date time using laravel

seema picture seema · Dec 13, 2018 · Viewed 26.2k times · Source

I want to store current date time in MySQL using following laravel function. Actually, I stored static date instead of this i want store current date time at created_at,updated_at field in database.

 function insert(Request $req)
   {
       $name=$req->input('name');
       $address=$req->input('address');
       $data=array("name" => $name,"address" => $address,"created_at"=>'2017-04-27 10:29:59',"updated_at"=>'2017-04-27 10:29:59');
       DB::table('student')->insert($data);
       echo "Record inserted successfully.<br/>";
       return redirect('/');
   }

Answer

Jignesh Joisar picture Jignesh Joisar · Dec 13, 2018

use laravel helper function

now()

other wise used carbon class

Carbon\Carbon::now()

used like that

$data = array("name" => $name,"address" => $address,"created_at"=> Carbon::now(),"updated_at"=> now());
DB::table('student')->insert($data);

for more information see