Some Useful Timestamp Tricks of Laravel
Hello artisan, today I’m going to share som timestamp tricks in Laravel. Let’s have a look:
Table of Contents
- Change Column Name
- Change Date/Time Format
- Order by Latest and Oldest
- Update Without Updating updated_at Column
- Update updated_at Column Only
- Carbon Supported
- Disable Timestamp
Change Column Name
If our database contains different columns instead of created_at
& updated_at
, we can define the names as timestamp in the model.
class User extends Model
{
const CREATED_AT = 'created_datetime';
const UPDATED_AT = 'updated_datetime';
// other properties and methods
}
Change Date/Time Format
By default, timestamps are formatted as 'Y-m-d H:i:s'
. If you need to customize the timestamp format, set the $dateFormat
property on your model. This property determines how date attributes are stored in the database:
class User extends Model
{
protected $dateFormat = 'U';
// other properties and methods
}
Order by Latest and Oldest
We can order data by timestamp using two shortcut methods. These are latest()
and oldest()
methods.
// latest
User::latest()->get();
// oldes
User::oldest()->get();
Update Without Updating updated_at Column
Normally, the update eloquent method automatically updates current timestamps. We can easily stop updating timestamps during updating data like this:
$user = User::find(1);
$user->email = "[email protected]";
$user->timestamps = false;
$user->save();
Update updated_at Column Only
This trick is the opposite of previous trick. We can set new value to only updated_at column.
$user = User::find(1);
$user->touch();
Carbon Supported
Without converting to Carbon instance, we can directly perform Carbon operations on timestamps. Have a look at this example:
# example 1
$user->created_at->addDays(7);
# example 2
$user->updated_at->diffForHumans()
Disable Timestamp
Suppose, we have a table that doesn’t contain created_at & updated_at columns. In this situation, if we run eloquent query, Laravel shows error. To solve, we have just disable timestamps for the model.
class User extends Model
{
public $timestamps = false;
// other properties and methods
}
That’s it. Thanks for reading. ?
Comment
Preview may take a few seconds to load.
Markdown Basics
Below you will find some common used markdown syntax. For a deeper dive in Markdown check out this Cheat Sheet
Bold & Italic
Italics *asterisks*
Bold **double asterisks**
Code
Inline Code
`backtick`Code Block```
Three back ticks and then enter your code blocks here.
```
Headers
# This is a Heading 1
## This is a Heading 2
### This is a Heading 3
Quotes
> type a greater than sign and start typing your quote.
Links
You can add links by adding text inside of [] and the link inside of (), like so:
Lists
To add a numbered list you can simply start with a number and a ., like so:
1. The first item in my list
For an unordered list, you can add a dash -, like so:
- The start of my list
Images
You can add images by selecting the image icon, which will upload and add an image to the editor, or you can manually add the image by adding an exclamation !, followed by the alt text inside of [], and the image URL inside of (), like so:
Dividers
To add a divider you can add three dashes or three asterisks:
--- or ***

Comments (0)