Disable Laravel's Eloquent timestamps

PhpLaravelEloquent

Php Problem Overview


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?

Php Solutions


Solution 1 - Php

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.

Solution 2 - Php

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

Solution 3 - Php

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

Solution 4 - Php

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.

Solution 5 - Php

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.

Solution 6 - Php

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 = '[email protected]';
$users->save();

Solution 7 - Php

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;

Solution 8 - Php

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

Solution 9 - Php

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

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

Solution 10 - Php

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionprojectxmattView Question on Stackoverflow
Solution 1 - Phpbgallagh3rView Answer on Stackoverflow
Solution 2 - PhppimarcView Answer on Stackoverflow
Solution 3 - PhpDumindu PereraView Answer on Stackoverflow
Solution 4 - PhpDumindu PereraView Answer on Stackoverflow
Solution 5 - PhprealplayView Answer on Stackoverflow
Solution 6 - PhpMd. Saidur Rahman MilonView Answer on Stackoverflow
Solution 7 - PhpJignesh JoisarView Answer on Stackoverflow
Solution 8 - PhpShahrukh AnwarView Answer on Stackoverflow
Solution 9 - PhpKrishanView Answer on Stackoverflow
Solution 10 - PhpSnapeyView Answer on Stackoverflow