How to prevent Eloquent from adding created_at or updated_at timestamps?

Laravel: How to prevent Eloquent from adding created_at or updated_at timestamps?

If you want to disable both created_at and updated_at then add this to your model:

  1. const UPDATED_AT = null;
  2. const CREATED_AT = null;

If you want to disable just one of those fields (i.e. turn off created_at but keep updated_at, or keep created_at but disable the updated_at field) then just add the one line as required.

Also remember to remove this from the migrations:

  1. $table->timestamps()

Note: There is also the DELETED_AT const, if you use SoftDeletes.

webdevetc profile pic
webdevetc

I am a 29 year old backend web developer from London, mostly focusing on PHP and Laravel lately. This (webdevetc.com) is my blog where I write about some web development topics (PHP, Laravel, Javascript, and some server stuff). contact me here.



More...


Comments and discussion about How to prevent Eloquent from adding created_at or updated_at timestamps?

Found this interesting? Maybe you want to read some more in this series?

Or see other topics in the Laravel language

Or see other languages/frameworks:
PHP Laravel Composer Apache CentOS and Linux Stuff WordPress General Webdev and Programming Stuff JavaScript
Or see random questions

How to make multiple routes share the same URL structure or URL parameters

How to sort an array of dates and times?

How to get table column names from a database table in Eloquent?

How to block IP addresses in Apache by using regular expressions (regex) in .htaccess

How to fix the MySQL related "Specified key was too long error" error in Laravel

When is __destruct called in PHP?

How do you run a PHP script from the command line?

What kind of database field type should you (normally) use for foreign keys?

How to check if a relation was loaded on an Eloquent model already?

How to disable file hotlinking in Apache with .htaccess