ChatGPT解决这个技术问题 Extra ChatGPT

Disable Laravel's Eloquent timestamps

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?


J
John

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.


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)
Wasn't aware of the withTimestamps() method. I'll need to look into it.
@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?
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.
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.
p
pimarc

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
{
    //
}

D
Dumindu Perera

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).


D
Dumindu Perera

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.


This worked for me because setting $timestamps = false caused created_at->diffForHumans() method to stop working since it's no longer a carbon instance.
It works for me, this method just disable the updated_at field that I need, thanks
r
realplay

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.


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.
M
Md. Saidur Rahman Milon

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();

k
koen

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;

S
Shahrukh Anwar

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

public $timestamps = false;


Z
Zak Henry

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

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

public function getUpdatedAtColumn()
{
    //Do-nothing
}

A case of "it works but no-one in their right mind would actually do this"
S
Snapey

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

关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now