Laravel Timestamp Tricks

There are various interesting things that a user can do with Laravel timestamp. Some of these are:

Change Timestamp Column Name

If the user has a non-laravel database as well as a differently named timestamp column then there is no need to panic.

Let’s say a user has created_time and updated_time in the timestamp column. Users can specify it in the model:

class Role extends Model
{
    const CREATED_AT = 'created_time';
    const UPDATED_AT = 'updated_time';

Disable Timestamps

If a user doesn’t have a time column but tries something like Model::create($arrayOfValues). As a result, he/she will get a SQL error.

To solve this problem, the user can disable the automatic timestamp by adding one property.

class Role extends Model
{
    public $timestamps = FALSE;
}

Before Laravel 7, the DateTime would be in the format of 2019-12-02 20:01:00. However now in Laravel 10, the date time is in the format of 2019-12-02T20:01:00.283041Z. So, if you need to customize timestamp format, set the $dateFormat property on your model which determines how attributes are stored in a database.

class Flight extends Model
{
    /**
     * The storage format of the model's date columns.
     *
     * @var string
     */
    protected $dateFormat = 'U';
}

Order Timestamp Latest and Oldest

Users can order data by timestamp using two shortcut methods which are:

User::latest()->get();

The above code is for getting the latest time. As for getting the oldest timestamp, the code is;

User::oldest()->get();

Many-to-Many: Pivot Table with Timestamps

When a user creates a pivot table in many-to-many relationships like table role_user between users and roles tables. Due to this a bit of exception occurs.

In the model, you would define relationships like:

class User extends Model
{
    public function roles()
    {
        return $this->belongsToMany(Role::class);
    }
}

Then, you would add a role to a user by:

$roleID = 1;
$user->roles()->attach($roleID);

The pivot tables do not contain timestamps by default. But if you do want to save the timestamps automatically, you need to add them to the migration file. Then define relationship using ->withTimestamps();

public function roles()
{
    return $this->belongsToMany(Role::class)->withTimestamps();
}

Timestamp Fields are Carbon by default

created_at and updated_at are casts as $dates of the Eloquent model by default. So, the user can perform carbon operations in them without converting in carbon instance.

$client->created_at->addDays(5);
now()->diffInDays($client->updated_at);

Update Without Operating updated_at

Laravel automatically saves the current timestamp into updated_at by default if the user updates the eloquent record.

In some cases, you would like to avoid it. E.g. you increment some value and don’t want to consider it as a “full record update”.

$user = User::find(1);
$user->profile_views_count = 123;
$user->timestamps = false;
$user->save();

Touch and Parent Touch

In some cases, you would like to set a new value to ONLY the updated_at column. So instead of using:

$user->update(['updated_at' => now()]);

You can use this process:

$user->touch();

Whereas in some cases, you would like to update the parent record along with the current eloquent model as well.

E.g., if some details were updated then you want to consider that posted record should have a new updated_at as well.

For that, you need to define parent touches in the eloquent model:

class Details extends Model {

    protected $touches = ['post'];

    public function post()
    {
        return $this->belongsTo('Post');
    }

}

Asmit Nepali, a Full Stack Developer, holds a Software Engineering degree from Gandaki College of Engineering and Science. Proficient in PHP Laravel, Vue.js, MySQL, Tailwind, Figma, and Git, he possesses a robust technical skill set.

Leave a Comment