277

I'm in the process of converting one of our web applications from CodeIgniter to Laravel. However at this moment we don't want to add the updated_at / created_at fields to all of our tables as we have a logging class that does all this in more depth for us already.

I'm aware I can set $timestamps = false; in:

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

However I'd rather not change a core file for Laravel, or have everyone of my models have that at the top. Is there any way to disable this elsewhere for all models?

projectxmatt
  • 3,091
  • 2
  • 15
  • 16

11 Answers11

508

You either have to declare public $timestamps = false; in every model, or create a BaseModel, define it there, and have all your models extend it instead of eloquent. Just bare in mind pivot tables MUST have timestamps if you're using Eloquent.

Update: Note that timestamps are no longer REQUIRED in pivot tables after Laravel v3.

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

John
  • 197
  • 1
  • 2
  • 22
bgallagh3r
  • 5,866
  • 1
  • 14
  • 17
  • 1
    Don't know why I never thought of doing a BaseModel and setting it there. Works beautifully. Just a note according to the documentation pivot tables need it only if setting withTimestamps() now (don't know if this changed from previous versions) – projectxmatt Nov 12 '13 at 19:29
  • Wasn't aware of the withTimestamps() method. I'll need to look into it. – bgallagh3r Nov 12 '13 at 19:39
  • @bgallagh3r, is there anyway to diasble only for perticuler query , Lets say for fontend display i dont want timestamp, but for backend i want time stamp. Is there anyway to do so? – user7747472 Jun 12 '18 at 18:25
  • 21
    When removing $table->timestamps() from the migration, eloquent will still include them in the query. This leads to an error as the field does not exists. Otherwise great answer. – Thijs Steel Jul 06 '18 at 08:31
  • Yes, this is still the case today (Laravel Framework v7.22.4). Found this thread trying to work around this issue. You have to explicitely disable the timestamps via `$timestamps = false;` to, e.g., seed a table that doesn't have any `created_at` and `updated_at` fields. – CLN Aug 13 '20 at 09:56
  • Creating a BaseModel is very good. As an added bonus, it allows, if one needs to use singular name tables (among other things) to redefine the database table naming strategy by redefining the getTable() method. – Bevelopper Mar 29 '21 at 17:28
  • Noob question: does it need to be public? All other variables in the model are of the protected type. – beeftony Nov 04 '21 at 08:48
202

Simply place this line in your Model:

public $timestamps = false;

And that's it!


Example:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    public $timestamps = false;

    //
}

To disable timestamps for one operation (e.g. in a controller):

$post->content = 'Your content'; 
$post->timestamps = false; // Will not modify the timestamps on save
$post->save();

To disable timestamps for all of your Models, create a new BaseModel file:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class BaseModel extends Model
{
    public $timestamps = false;

    //
}

Then extend each one of your Models with the BaseModel, like so:

<?php

namespace App;

class Post extends BaseModel
{
    //
}
pimarc
  • 3,621
  • 9
  • 42
  • 70
50

If you are using 5.5.x:

const UPDATED_AT = null;

And for 'created_at' field, you can use:

const CREATED_AT = null;

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

Dumindu Perera
  • 1,571
  • 15
  • 13
25

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

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

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

Dumindu Perera
  • 1,571
  • 15
  • 13
17

In case you want to remove timestamps from existing model, as mentioned before, place this in your Model:

public $timestamps = false;

Also create a migration with 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 allow rolling back.

realplay
  • 2,078
  • 20
  • 32
  • 2
    This should be the best answer. If you have an existing production database, you'd need to run a migration as well as disable timestamps on the model. – brad Feb 09 '19 at 19:04
15

Eloquent Model:

class User extends Model    
{      
    protected $table = 'users';

    public $timestamps = false;
}

Or Simply try this

$users = new Users();
$users->timestamps = false;
$users->name = 'John Doe';
$users->email = 'johndoe@example.com';
$users->save();
12

Add this line into your model:

Overwrite existing variable $timestamps true to false

/**
 * Indicates if the model should be timestamped.
 *
 * @var bool
 */

public $timestamps = false;
koen
  • 5,383
  • 7
  • 50
  • 89
Jignesh Joisar
  • 13,720
  • 5
  • 57
  • 57
8

just declare the public timestamps variable in your Model to false and everything will work great.

public $timestamps = false;

Shahrukh Anwar
  • 2,544
  • 1
  • 24
  • 24
3

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

public function setUpdatedAt($value)
{
   //Do-nothing
}

public function getUpdatedAtColumn()
{
    //Do-nothing
}
Zak Henry
  • 2,075
  • 2
  • 25
  • 36
Krishan
  • 97
  • 2
2

You can temporarily disable timestamps

$timestamps = $user->timestamps;
$user->timestamps=false;   // avoid view updating the timestamp

$user->last_logged_in_at = now();
$user->save();

$user->timestamps=$timestamps;   // restore timestamps
Snapey
  • 3,604
  • 1
  • 21
  • 19
0

just use this code on your model

 protected $hidden = [
    'created_at',
    'updated_at',
];
  • 1
    I recommend that you don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Mark Rotteveel May 03 '23 at 08:02