Laravel and MySQL do not play well in case of DateTime

I created a webapp using Laravel and SQLite, everything is fine. Now I want to use MySQL as a database, but now I can not add data to my tables.

This is the field from my migration:

$table->dateTime('last_updated')->nullable();

and this is how I fill the field using laravel:

$client->last_updated = time();

But this gives me an error, for example:

Illuminate \ Database \ QueryException
SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '1402140378' for column 'last_updated' at row 1 (SQL: update `clients` set `last_updated` = 1402140378 where `id` = 1)

Any help would be appreciated.

+4
source share
1 answer

$table->dateTimecreates a column DATETIMEin your database that expects a value YYYY-MM-DD HH:ii:ss. What you insert is a unix timestamp.

You can use Carbon(required use Carbon\Carbonat the top of the file)

$client->last_updated = Carbon::now()

date(...),

$client->last_updated = date('Y-m-d H:i:s') 

or PHP DATETIMEto insert the current time into the database.

+13
source

Source: https://habr.com/ru/post/1543659/


All Articles