Insert the current date time using Laravel [duplicate]
Asked Answered
P

5

15

I want to store the current date time in MySQL using the following Laravel function. Actually, I stored a static date. Instead of this, how can I store the current date time in the created_at and updated_at fields in the 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('/');
}
Polenta answered 13/12, 2018 at 4:43 Comment(2)
Solution is already available @ #28109679Galba
yes i checked but error occur at Carbon\Carbon namespace so that i asked question ..Polenta
M
28

Use the Laravel helper function

now()

Otherwise, use the carbon class:

Carbon\Carbon::now()

It is used like this:

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

For more information, see now()

Menderes answered 13/12, 2018 at 4:45 Comment(0)
D
3

You can also use this to get the current date time. It's working.

$current_date = date('Y-m-d H:i:s');

And also I have updated and set things in your function. You have to add this:

function insert(Request $req)
{
    $name = $req->input('name');
    $address = $req->input('address');

    $current_date_time = date('Y-m-d H:i:s');

    $data = array("name" => $name,
                  "address" => $address,
                  "created_at" => $current_date_time,
                  "updated_at" => $current_date_time);
    DB::table('student')->insert($data);
    echo "Record inserted successfully.<br/>";
    return redirect('/');
 }
Donadonadee answered 13/12, 2018 at 5:5 Comment(1)
Notice that this solution will use the PHP server date-time, not necessarily the same date-time as the database server you would get using now() in MySQL or getDate() in SQLServer.Zworykin
N
3

Use this in your database query:

'created_at' => Carbon::now(),
'updated_at' => Carbon::now()
Neuter answered 13/12, 2018 at 6:7 Comment(0)
M
1

You can use the DateTime object.

Look at the below code:

$curTime = new \DateTime();
$created_at = $curTime->format("Y-m-d H:i:s");

$updateTime = new \DateTime();
$updated_at = $updateTime->format("Y-m-d H:i:s");
Miniskirt answered 13/12, 2018 at 4:48 Comment(0)
T
-1

Use

use Carbon\Carbon;

at the header of the controller. This code work for Laravel 9:

$updated = Carbon::now();

You may save it inside a variable and use the variable for inserting and updating statements or for any other purpose.

During migration, I defined the column check_at as datetime. And I used the following command for table creation.

php artisan migrate

It generates a timestamp-type column in MySQL. I am passing the variable $updated for update purposes. While using phpMyAdmin, I can see the column check_at has data in date-time format.

Tyndareus answered 24/6, 2022 at 2:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.