Laravel developed by Taylor Otwell, is a powerful MVC PHP framework. It is designed for developers who require a smart and simple toolkit to build full-featured web apps. A user can do many interesting things with laravel timestamp. Want to learn some timestamp tricks in Laravel. Let’s begin:
(1) Disable Timestamps
If your DB table does not comprise timestamp fields created _at and updated _at, you can stipulate that Eloquent model wouldn’t use them, with $timestamps = false property.
(2) Change Timestamp Column Name
Even if you are working with a non-Laravel database and your timestamp columns are named differently? Maybe, you have create _time and update _time. Also, you can specify them in the model, too:
(3) Customize Your Timestamp Format
If you are willing to modify the format of your timestamps, then set the $date Format property on your model. This property determines how date attributes are stored in the database, as well as their format when the model is consecutive to an array or JSON.
(4) Many-to-Many: Pivot Table with Timestamps
Pivot table is small exception for timestamps, like table role _user between users and roles tables. In the model, you would outline relationship like this: And then, when you wish to add a role to a user, you can do something like this: By default, those pivot tables do not contain timestamps. And Laravel does not try to fill in created _at/updated _at in this situation. You can save the timestamps automatically all you need to do is to add them into migration file, and then define relationship using ->withTimestamps();
(5) Order by Timestamp with latest () and oldest ()
If you wish to order data by timestamps there are two “shortcuts”. Instead of: You can do it faster: By default, latest () will order by created_at. There is a contrary method oldest () which would order by created_at ascending.Also, you can mention another column to order by. For e.g., if you want to use updated_at, you can do this:
(6) Update without touching updated _at Column
Generally, the update eloquent method automatically updates current timestamps. One can easily stop updating timestamps during updating data like this:
(7) Touch and Parent Touch
Contradictory of the last e.g. – maybe you wish to set new value to ONLY updated_at column, without changing others. So, instead of: You can also choose to use a shorter method: Let’s now consider a different case – sometimes you want to not only set updated _at of current eloquent model, but also its parent record by relationship. Say for e.g., if some comment was updated, then you want to consider that post record should have new revised _at, too. Then, you need to define “parent touches” models in the eloquent model:
(8) Timestamp Fields are Carbon Automatically
By default, both created_at and updated_at are casts as $dates of Eloquent model, so you can perform Carbon operations on them, without converting to Carbon instance. For example: So, that’s it these are some of the important tips. Hope you like the content. Share your comments and feedback on the above post.