7
votes

I am new to laravel. Recently, I have created my ER diagram for my app. While learning laravel I see that they have this timestamp property in the schema builder that creates a created_at and updated_at column. Base on my modeling I don't really need these extra columns, so it's a must to have them or what are the benefits of having these columns on every table.

3

3 Answers

6
votes

A benefit for having them, in cases where you need them, is that Eloquent will automatically update these fields. So lets say you update a model, Eloquent will automatically set the updated_at, leaving you with less code to write, maintain and think about.

As it is not a requirement to have these timestamps on every table, you can simply disable them by using public $timestamps = false; on the related model as such:

class User extends Eloquent {

    protected $table = 'users';

    public $timestamps = false;

}
1
votes

Well, timestamps allows you to track about when the record has been updated recently. But its not mandatory in laravel. Certainly, there are times when you really don't need these columns. If you don't want to use the timestamps, remove it from the migration file and disable created and updated at columns by specifying public $timestamps = false in your respective model.

1
votes

How to prevent timestamps usage in a model?

First of all to stop using the timestamps in a table don't include $table->timestamps() in your Schema::table() or Schema::create() implementation, then in every corresponding model class that has no timestamps included in its table write protected $timestamps = false;

What is the purpose of timestamps in Laravel?

The purpose of the timestamps is to track the date and time at which new records are inserted or updated so, whenever you insert a new record (which means creating a new model) the created_at and updated_at columns will be automatically filled with the current date and time, and every time you update a record its updated_at column gets updated with the date and time of the update.

I don't know which version of Laravel you are using but, here's the Eloquent Model documentation and, the migration documentation too for your reference.

Please try to go through the Laravel documentation carefully, it will give you a concrete understanding of how things work, and how to go through your development with it easily.