14

I am using the standard updated_at and created_at on a table.

My results are ordered by updated_at since they can be edited.

However, on a page I am updating a row counter (views). This means that the updated_at will be updated with the new date/time but I want to prevent this.

Is there any way to do this? Or am I going to have to use created_at to order my results?

I would rather not use my own manual created_at and updated_at

Cjmarkham
  • 9,484
  • 5
  • 48
  • 81
  • `class ModelName extends Eloquent { public static $timestamps = false; }` – SoWhat Jul 02 '14 at 10:29
  • That would just remove them, I want to keep them but prevent updating the `updated_at` for a specific query – Cjmarkham Jul 02 '14 at 10:32
  • 1
    Never mind, found the answer here [http://stackoverflow.com/questions/18904853/update-without-touching-timestamps-laravel](http://stackoverflow.com/questions/18904853/update-without-touching-timestamps-laravel) – Cjmarkham Jul 02 '14 at 10:35

1 Answers1

20

For single model queries:

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

For multiple model queries use

DB::table('products')->...->update(...)

instead of

Product::...->update(...)
abmblob
  • 361
  • 2
  • 14
Luca C.
  • 11,714
  • 1
  • 86
  • 77
  • You can do `$product->fill($fields); $product->save(['timestamps' => false]);` and encapsulates this into a utilities class (e.g. `\App\Utils\Models\Products::saveProductNoUpdateAt(\App\Models\Product $product, array $fields)`). Then you have no `DB` (no model abstraction) but still a single place for deactivating auto-update of `updated_at`. – Roland Oct 19 '18 at 13:30
  • 1
    Remove "on update current timestamp" from DB – syed mahroof Apr 01 '19 at 04:21