Laravel Update a Record Without Refreshing Timestamp

By | December 23, 2022

In Laravel, you can use the following steps to update a record in the database without updating the timestamp fields:

  1. Retrieve the record you want to update using the model’s find method or the Eloquent query builder. For example:
$user = User::find(1);

2. Modify the record’s attributes as needed. For example:

$user->name = 'John Smith';
$user->email = 'john@example.com';

3. Save the record to the database using the model’s save method and passing the $timestamps parameter as false. This will prevent Laravel from updating the created_at and updated_at timestamps. For example:

$user->save($timestamps = false);

Alternatively, you can use the model’s update method and pass the $timestamps parameter as false to achieve the same result. For example:

$user->update(['name' => 'John Smith', 'email' => 'john@example.com'], $timestamps = false);

Note that this method will only work if you have the $timestamps property set to true in your model’s class. If you have set it to false, the timestamps will not be updated regardless of the $timestamps parameter.