Laravel - How to disable created_at and updated_at timestamps from Model?

About Us:

We are from free coder. We are a group of web developers who are passionate about web development. You can learn from us php, laravel, node js, vue js, react js and many other topic on web developement.
we are very happy to help you.
free coder

Today what you are going to learn:

As we know, we create new record in table using model then created_at and updated_at column value automatic set current timestamps. But sometime you require to prevent set timestamp of created_at and updated_at column, Maybe you don't have. So in this post we will learn how to make disabled timestamps value from model in laravel 6, laravel 7 and laravel 8 project too.

So, there are two way to create disable created_at and updated_at timestamps in laravel 5 application. So first you can simply "$timestamps" equals to false in your model. So you can do it as simple bellow lesson of Item model.

app/Item.php

<?php


namespace App;


use Illuminate\Database\Eloquent\Model;


class Item extends Model

{

protected $fillable = ['title','content'];


public $timestamps = false;

}

Now, we can also use another way to update timestamps value of created_at and update_at. In this bellow lesson you can modify value of created_at and updated_at column value as bellow lesson:

app/Item.php

<?php


namespace App;


use Illuminate\Database\Eloquent\Model;


class Item extends Model

{

protected $fillable = ['title','content'];


public function setUpdatedAt($value)

{

return NULL;

}


public function setCreatedAt($value)

{

return NULL;

}

}

thank you very much for following up with me.

Recent Posts

Related Posts