How to disable model timestamps in Laravel

Hello Geek, When we need to disable the created at and updated at timestamps on a model in Laravel, we can do so easily by using the $timestamps model variable. It is a minor detail, but it is critical to comprehend and apply. You can also use it in applications written in Laravel 6, 7, 8, and 9.

When you create a new item or user with the model, the created at and updated at columns are set to the default time, but you can prevent this by setting the $timestamps variable to a false value.

I’m making new records with the model’s create method, as shown below:

Item::create(['title'=>'ItSolutionStuff.com']);

The above code simply adds new records to the items table with the current timestamps in created at and updated at. However, you can avoid this by setting the $timestamps variable to false. So your model will look like this:

<?php


namespace App;


use Illuminate\Database\Eloquent\Model;


class Item extends Model
{


    public $fillable = ['title'];


    public $timestamps = false;


}

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

All the best nerd!

Leave a Reply