简体   繁体   中英

Laravel Eloquent returns wrong created_at and updated_at values

I am retrieving data from a MySQL database. The created_at and updated_at value in the database is different from what I got when I tried to query the data using Eloquent.

I have tried setting the timezone by using date_default_timezone_set() but the problem still persists.

Here is the query in my Controller,

$a = FooModel::whereDate('created_at', '=', $date);

In my case, date is date("Ymd")

The value of created_at in the database is 2019-07-22 13:00:17 but the value of created_at in $a is 2019-07-23 00:00:17

This also applies to updated_at

I have resolved this by creating my own timestamps, DO NOT use the $table->timestamps() on your migration create your own timestamp instead like: $table->timestamp('created_on')

I figured out that laravel is doing something upon retrieving the default created_at and updated_at I dont know what it is but, that's why if you were to get their value on the database it returns same date(mostly) but different time.

also make sure to disable the default timestamps on your model by using: public $timestamps = false; if you were to make your own timestamp.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM