Disable Laravel Rouquent timestamps

I am in the process of converting one of our web applications from CodeIgniter to Laravel. However, at the moment we do not want to add the updated_at / created_at fields to all our tables, since we have a logging class that does all this in more detail for us already.

I know that I can set $timestamps = false; at:

 Vendor\laravel\framework\src\illuminate\Datebase\Eloquent\Model.php 

However, I would prefer not to change the main file for Laravel, or all of my models have what's on top. Is there a way to disable this elsewhere for all models?

+158
php eloquent laravel
Nov 12 '13 at 18:54
source share
9 answers

You either need to declare public $timestamps = false; in each model, or create a basic model, define it there, and all your models expand it, not eloquently. Just the head in the pivot tables MUST have timestamps if you use Eloquent.

Update: note that timestamps are no longer required in pivot tables after Laravel v3.

Update. You can also turn off timestamps by removing $table->timestamps() from your migration.

+314
Nov 12 '13 at
source share

Just put public $timestamps = false; in his model.

+93
Nov 01 '14 at
source share

If you only need to disable updated_at updating, just add this method to your model.

 public function setUpdatedAtAttribute($value) { // to Disable updated_at } 

This will override the parent method setUpdatedAtAttribute (). created_at will work as usual. Similarly, you can write a method to disable updating only created_at.

+22
Sep 11 '14 at 10:12
source share

If you are using 5.5.x:

 const UPDATED_AT = null; 

And for the "create_at" field you can use:

 const CREATED_AT = null; 

Make sure you are using the latest version. (This was broken in Laravel 5.5.0 and again fixed in 5.5.5).

+18
Jul 17 '18 at 6:43
source share

Elegant Model:

 class User extends Model { protected $table = 'users'; public $timestamps = false; } 

Or just try

 $users = new Users(); $users->timestamps = false; $users->name = 'John Doe'; $users->email = 'johndoe@example.com'; $users->save(); 
+8
Oct 21 '17 at 19:38 on
source share

If you want to remove timestamps from an existing model, as mentioned earlier, put this in your model:

 public $timestamps = false; 

Also create a migration with the following code in the up() method and run it:

 Schema::table('your_model_table', function (Blueprint $table) { $table->dropTimestamps(); }); 

You can use $table->timestamps() in your down() method to enable rollback.

+7
Aug 31 '18 at 19:29
source share

just declare the public timestamps variable in your Model as false and everything will work just fine.

public $timestamps = false;

+5
Jul 04 '18 at 7:50
source share

add this line to your model

overwrite existing $timestamps true to false

 /** * Indicates if the model should be timestamped. * * @var bool */ public $timestamps = false; 
+3
Apr 16 '19 at 6:25
source share

Override the setUpdatedAt() and getUpdatedAtColumn() functions in your model

 public function setUpdatedAt($value) { //Do-nothing } public function getUpdatedAtColumn() { //Do-nothing } 
+1
Jan 10 '15 at 9:06
source share



All Articles