Tricks with Laravel Timestamps
Learn some timestamp tricks in LaravelLaravel 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 TimestampsIf 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 NameEven 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 FormatIf 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 TimestampsPivot table is small exception for timestamps, like table role _user between users and roles tables. In the model, you would outline relationship like this:
(5) Order by Timestamp with latest () and oldest ()If you wish to order data by timestamps there are two “shortcuts”. Instead of:
(6) Update without touching updated _at ColumnGenerally, the update eloquent method automatically updates current timestamps. One can easily stop updating timestamps during updating data like this:
(7) Touch and Parent TouchContradictory of the last e.g. – maybe you wish to set new value to ONLY updated_at column, without changing others. So, instead of:
(8) Timestamp Fields are Carbon AutomaticallyBy 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.
I have observed that in the world the present moment, video games include the latest craze with kids of all ages. There are times when it may be difficult to drag your family away from the video games. If you want the best of both worlds, there are various educational video games for kids. Good post.