Laravel Timestamp Tricks

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

Change Timestamp Column Name

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

Lets say user has created_time and updated_time in timestamp column. User can specify it in model:

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

Disable Timestamps

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

For solving this problem, user can disable automatic timestamp by adding one property.

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

Before Laravel7, the datetime would be in format of 2019-12-02 20:01:00. However now in laravel7, date time is in 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 database.

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

Order Timestamp latest and oldest

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

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

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


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

Many-to-Many: Pivot Table with Timestamps

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

In model, you would define relationship like:

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

Then, you would add role to user by:

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

The pivot tables does not contain timestamp by default. But if you do want to save the timestamps automatically, you need to add them into 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 Eloquent model by default. So, the user can perform carbon operation 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 current timestamp into updated_at by default if the user updates eloquent record.

In some cases, you would like to avoid it. For e.g. you increment some value and don’t want to consider it as “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 new value to ONLY 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 parent record along with current eloquent model as well.

For e.g. if some details was updated then you want to consider that post record should have new updated_at as well.

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

class Details extends Model {

    protected $touches = ['post'];

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

}
Total
0
Shares
Leave a Reply

Your email address will not be published.

Related Posts
Total
0
Share