Blog Detail

03

Aug
Some Tips for the Customization of Laravel Timestamps  cover image

arrow_back Some Tips for the Customization of Laravel Timestamps

In this blog, I will explain some quick tricks that will assist you in customizing Laravel Timestamps so that you can work with timestamps effortlessly.

In Default Laravel, Eloquent models consider that your table has Timestamp fields “created_at” and “updated_at”. But there are many techniques that you can follow to customize them or perform some beneficial operations. Let’s check out.

Change Default TimeStamps Columns Name

If you are working with a non-laravel Table and your TimeStamps fields are differents such as created_time and updated_time.Then you can change it from the model as given below.’

class Product extends Model
{
    const CREATED_AT = 'added_time';
    const UPDATED_AT = 'update_time';
}

Disable Timestamps

If your Database table does not have those fields, and you are trying to create or update a record with eloquent models like Product::create($data); or product>update(product->update(data). Then you will get an error for those fields.

You can disable that automatic timestamps in your Eloquent Model. You have to add just one property:

class Product extends Model
{
    public $timestamps = FALSE;

    //  other model properties and methods
}

Change the default format of Timestamp Date/Time

By default, the Laravel TimeStamps value looks like this “2021-08-26 04:07:31” but if you want to change the format of TimeStamp value, You can do it by using accessor and Mutator methods in your model.

 class Product extends Model
 {
   /*    used for  accessing  records   */
  public function getCreatedAtAttribute($date)
  {
    return Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $date)->format('Y-m-d');
   }

   public function getUpdatedAtAttribute($date)
    {
    return Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $date)->format('Y-m-d');
   }
 
  /*  used for updating  records   */
  public function setCreatedAtAttribute($date)
   {
    return Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $date)->format('Y-m-d');
   }

  public function setUpdatedAtAttribute($date)
  {
    return Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $date)->format('Y-m-d');
  }

Order by Timestamp with latest() and oldest()

You can use two ways to order data by timestamps.
Instead of:

Product::orderBy('created_at', 'desc')->get();

You can do it quickly:

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

By default, the latest() will order by created_at.
There is an opposite method oldest() which you could utilize to order by created_at ascending.

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

Also, you can define another column to order by. For example, if you want to use updated_at, you can do this:

$lastUpdatedProduct = Product::newest('updated_at')->first();

Update Any Column Without Updating updated_at Column

Usually, In the Laravel, the update Eloquent method automatically updates current timestamps. You can effortlessly stop the updating timestamps during updating data like this:

$product= Product::find(1); 
$product->category= "clothes"; 
$product->timestamps = false;
$product->save();

I hope these tips will help you in your projects.

Published at : 03-08-2021

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project