Hareesh
Hareesh

Reputation: 1587

Why Laravel provides default timestamps?

When we create a new Model or database migration in Laravel we can use $table->timestamps(); which creates created_at and updated_at by default. Why does a project required these fields in the view of a best project structure?

Upvotes: 3

Views: 1710

Answers (3)

Yogendrasinh
Yogendrasinh

Reputation: 895

create_at and updated_at is very useful when there's multiple roles in your system. In my project i have multiple roles so i have added columns like updated_by and created_by with laravel's created_at and updated_at i can find on which date and time it's created OR updated and by my columns created_at and updated_at i can find out which user created record and which user updated record.

Also laravel timestamp automatically update the record column at the time of update so you don't need to worry about it to write code for change the update_at column in table.

So basically for tracing the timestamp is very useful in your project.

If you don't need it in every table then you can simply avoid it using below line in you model.

public $timestamps = false;

Upvotes: 0

Prashant Deshmukh.....
Prashant Deshmukh.....

Reputation: 2292

Every Model/Records you insert in table it`s better to track its created_at time and updated_at time. Timestamp use cases could be different depending upon indiviusal.

Most developer use timepstamp to track their database records.

That`s why Laravel Eloquent by default provide created_at and updated_at options. Whenever Model/Records get created created_at column value get set automatically and whenever record get updated updated_at column value set to that time automatically, so developer need to write minimum code for managing timestamps.

Laravel timestamps are optional use public $timestamps = false; in Model to disable timestamp.

Upvotes: 2

GiamPy
GiamPy

Reputation: 3560

It's always useful to know when an object is created or updated. Even though I personally prefer creating different timestamps for different purposes, like if I have an entity which gets processed asynchronously, I usually add a datetime column called completed_at so that I know when the job was completed.

If you don't like default timestamps, you can remove them by assigning public $timestamps = false; in your Eloquent model.

Upvotes: 2

Related Questions