Reputation: 6081
I'm using Laravel 5.1. To make it simple, I have the following code
Migration:
Schema::create('sitemap_data', function (Blueprint $table) {
// Primary and foreign keys
$table->increments('id');
$table->string('postUrl');
// Database functions
$table->timestamps();
});
And this is the code somewhere else I'm using
$sitemapData = SitemapData::firstOrNew([
'postUrl' => $post
]);
$sitemapData->save();
Now, according to the Laravel documentation
Again, the updated_at timestamp will automatically be updated, so there is no need to manually set its value
The updated_at value should be updated in the table. However, this is not happening.
It get's only set on the first insert, but not on updating. When I do it manually, like this
$sitemapData = SitemapData::firstOrNew([
'postUrl' => $post
]);
$sitemapData->updated_at = Carbon::now();
$sitemapData->save();
it works. However, the docs say this should happend automatically, so what is wrong here?
I've searched some sites on stackoverflow for this question, but the ones I found where for Laravel 3 or 4.1, 4.2 etc.
How would I do that correctly?
Upvotes: 22
Views: 51225
Reputation: 4151
go to phpmyadmin and select your db and select the table tap on SQL tab above and type for example something like this:
UPDATE `boshogriq_table` SET `updated_at`= DATE_ADD(`created_at`, INTERVAL 1 MINUTE)
WHERE `updated_at` > DATE_ADD(`created_at`, INTERVAL 10 MINUTE)
run it by go button. Laravel won't allow you do this
Upvotes: 0
Reputation: 2325
This is not the case in the question, but same issue will happen if you're using Query Builder instead of Eloquent Model.
If you do
DB::table('users')->where()->update()
instead of
User::where()->update()
updated_at will not be updated.
Upvotes: 7
Reputation: 134
Yeah what Musterknabe has done is correct but he should also check his model SitemapData.php it should have to have set $timestamps = true;
<?php
class SitemapData extends Eloquent {
public $timestamps = true;
}
Upvotes: 5
Reputation: 6237
It is possible actually:
$model->updated_at = Carbon::now();
$model->save(['timestamps' => FALSE]);
That will properly save the updated_at
to now. If you're not sure if any of the model's columns have changed, but you do want to update the updated_at
regardless - this is the way to go.
Upvotes: 8
Reputation: 13325
As mentioned in comments, if the model did not change the timestamps wont be updated. However if you need to update them, or want to check if everything is working fine use $model->touch()
- more here
Upvotes: 55