Skip to content
On this page

Timestamps

When using Quick Models, Larawiz automatically adds timestamps, so there is no need to declare them inside the model definition.

yaml
models:
  Post:
    title: string
    excerpt: string
    body: longText
1
2
3
4
5
php
Schema::create('posts', function (Blueprint $table) {
    $table->id();
    $table->string('title');
    $table->string('excerpt');
    $table->longText('body');
    $table->timestamps();
});
1
2
3
4
5
6
7

Timestamps with time zone

You don't need time zones

Before going for timestamps with time zone, consider the following:

  • PHP, like most languages, is aware of timezone shifts and DST in the future or past.
  • Laravel always saves dates converted to UTC into the database.

In most scenarios, using time zone is not needed unless you expect to work on edge cases.

When using Quick Models, you can always use timestampsTz in the columns definitions to swap the normal timestamps to ones with time zone data.

yaml
models:
  Post:
    title: string
    body: string

  Comment:
    body: string
    timestampsTz: ~
1
2
3
4
5
6
7
8
php
Schema::create('posts', function (Blueprint $table) {
    $table->id();
    $table->string('title');
    $table->string('body');
    $table->timestamps();
});

Schema::create('posts', function (Blueprint $table) {
    $table->id();
    $table->string('body');
    $table->timestampsTz(); // Overridden from `timestamps`.
});
1
2
3
4
5
6
7
8
9
10
11
12

Disable Timestamps

To disable timestamps, use a Custom Model. If the Custom Model doesn't include either timestamps or timestampsTz inside the columns declarations, timestamps will be disabled for the model.

yaml
models:
  Post:
    columns:
      id: ~
      title: string
      excerpt: string
      body: longText
1
2
3
4
5
6
7
php
class Podcast extends Model
{
    public $timestamps = false;
}
1
2
3
4
php
Schema::create('posts', function (Blueprint $table) {
    $table->id();
    $table->string('title');
    $table->string('excerpt');
    $table->longText('body');
});
1
2
3
4
5
6

Custom Timestamps

Only under a Custom Models you can change the default columns for timestamping using the timestamps key.

For example, we can disable the default timestamps for updates by setting it as null and point the column to use as creation date.

yaml
models:
  Post:
    columns:
      id: ~
      title: string
      excerpt: string
      body: longText
      creation_date: timestamp nullable

    timestamps:
      created_at: creation_date
      updated_at: ~
1
2
3
4
5
6
7
8
9
10
11
12
php
class Podcast extends Model
{
    protected const CREATED_AT = 'creation_date';
    protected const UPDATED_AT = null;
}
1
2
3
4
5
php
Schema::create('posts', function (Blueprint $table) {
    $table->id();
    $table->string('title');
    $table->string('excerpt');
    $table->longText('body');
    $table->timestamp('creation_date')->nullable();
});
1
2
3
4
5
6
7