How to disable created_at and updated_at timestamps in Laravel Model?

How to disable created_at and updated_at timestamps in Laravel Model?

In this post we will give you information about How to disable created_at and updated_at timestamps in Laravel Model?. Hear we will give you detail about How to disable created_at and updated_at timestamps in Laravel Model?And how to use it also give you demo for it if it is necessary.

By default, Laravel Eloquent maintain two columns created_at and updated_at in your database tables automatically you just need to add these two columns in your tables and Eloquent will take care of the rest.

If you create table by following Laravel special schema builder then it create by default these two columns by using $table->timestamps();.This is very useful sometime to deal with last edit data on a specific table


Sometime if this is not so useful and you do not want that Eloquent will maintain these two columns then you can disable it by adding following timestamp property false to your model :

  1. class Product extends Eloquent {
  2. protected $table='products';
  3. public $timestamps= false;
  4. }
class Product extends Eloquent {

    protected $table = 'products';

    public $timestamps = false;

}

You can check if it is working or not then save any record in table and see these two columns will have null value if you set timestamps property false.


If you want to disable one timestamps for updated_at only then you can write a mutator for the same column which you want to disable.

  1. public functionsetUpdatedAt($value)
  2. {
  3. // Do nothing.
  4. }
public function setUpdatedAt($value)
{
    // Do nothing.
}

Same you can do with created_at column if you need to disable only created_at column.

Try this when you need it in your application.

Label :

PHP

Laravel PHP Framework

How To

MVC

Web Development

Hope this code and post will helped you for implement How to disable created_at and updated_at timestamps in Laravel Model?. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

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

6  +  2  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US