How to update a record without updating timestamp in Laravel (Eloquent)

Updated on 26-Nov-2021 in Questions
348

Yes! There is a way to prevent timestamp update when you update the record using Eloquent in Laravel. 

The common case for the need is to prevent the timestamp change if a non-public field is to be updated in the database e.g. maintaining views in the post table and incrementing it every time the post is loaded.

So here is the example code:

$post = Post::find(1);

// Prevents timestamps auto-update
$post->timestamps = false;

$post->increment('views');

$post->save();

The above operation is complete without updating the timestamps.

Published by:

Laravel Fan

Was this article helpful for you?

Yes No