Use only created_at as default timestamp

In Laravel deleted_at, created_at and updated_at fields are created as default when the table is created. We are going to see how to use only one timestamp in your project. 

const CREATED_AT = 'created';

const UPDATED_AT = 'updated';

If we add public $timestamps = false; all the default timestamps will get disabled. Unfortunately Eloquent does not provide functionality to disable some and keep only the one we want. So we need to think out of the box.

class User extends Eloquent {

 

    public $timestamps = false;

 

    public static function boot()

    {

        parent::boot();

 

        static::creating(function ($model) {

            $model->created_at = $model->freshTimestamp();

        });

    }

 

}

The code above will keep the created_at timestamp as static and the other default timestamps disabled.