How to disable model timestamps in Laravel?

In this post we will explain of how to disable model timestamps in Laravel. We require to disable created_at and updated_at timestamps on model on Laravel, So we can do it simply by using $timestamps variable of model. 

In this post I will share a simple method to manage laravel model to without timestamps. In Laravel Eloquent models automatically maintain created_at and updated_at timestamps. If you want to disable them, you can use the $timestamps property in your model.

When you create new category or using model at that time created_at and updated_at column set default time by default but you can prevent to set false value of $timestamps variable.

I am creating new category records using create method of model like as below example.

Category::create([
    'name' => 'xyz'
]);

By default, Laravel automatically update the created_at and updated_at columns in database table whenever records are inserted or updated. If you don’t need these timestamps, you can disable them your eloquent model. 

Disable timestamps for a model

To disable timestamps for a specific model, set the $timestamps property to false.

app/Models/Category.php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    public $fillable = ['name'];
    
    public $timestamps = false;
}

Now, you can check, created_at and updated_at will be null.

I hope this tutorial help you.

Leave a Reply

Your email address will not be published. Required fields are marked *