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. 

Using timestamps = false in Laravel Model.

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

Make sure place the $timestamps = false; property inside the mode class, like below.

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. This tells eloquent to not expected or update created_at and updated_at field.

Laravel disable timestamps created_at and updated_at

To disable both created_at and updated_at columns in a model, just set the $timestamps property to false:

class Category extends Model
{
    public $timestamps: false;
}

This method is the disable Laravel’s eloquent timestamps entirely.

I hope this tutorial help you.

Jasminkumar Borad
Jasminkumar Borad

I'm a full-stack developer, enterpreneur and owner of webstuffsolution.com. I live in india and i like to write tutorial and tips that can help to other user. I like to share various programming language tips and solution. I believe in hardworking and consistency.

Leave a Reply

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