Home Laravel How to disable model timestamps in Laravel?

How to disable model timestamps in Laravel?

by Shahriar Sagor

Today now in this post i will show you How to disable model timestamps in Laravel? Sometimes we may need to disable created_at and updated_at timestamps on the Model inLaravel application. So we can easily do this by using the $timestamps variable of model. It is a very small things but it is very important to understand and need to know how to use it. We can also use this in any version laravel 6, laravel 7 and laravel 8 application.

When we create a new item or user by using the model at that time the created_at and updated_at column set the default time by default but we can prevent to set it false value of $timestamps variable.

Here I am creating a new records by using create method of model as like as bellow example:

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

Ok, above code will simply to add a new records on items table also with current timestamps value in created_at and also updated_at. But we can prevent by add $timestamps variable value is false. So we can do by using our model will be like this way:

app/Model/Item.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Item extends Model
{
    public $fillable = ['title'];
    public $timestamps = false;
}

Read Also: How to implement infinite ajax scroll pagination in Laravel?

Thanks for read. I hope it help you. For more you can follow us on facebook

close

You may also like