I recently searched Stackoverflow for trending post topics and found a lot of people asking questions related to how to work with date in Laravel. In this article I will try to answer the main ones, as well as describe the processes associated with saving date to a database using the Eloquent ORM.

Migrations with creation, updation and deletion dates

There are many methods in Laravel migrations that will allow you to create the required columns in the database. There are also some very convenient ones for working with dates.

timestamps – will create created_at and updated_at columns of type Timestamp in your table. They are convenient to record in the table the time of row creation and their modification.

softDeletes – will create a deleted_at column of type Timestamp in your table. This column is used by the Eloquent ORM to “soft delete” data from the table.

In migration, the listed methods are used as follows.

Schema::create('persons', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->string('name');
    $table->timestamps();
    $table->softDeletes();
});

This code will create a “persons” table in the database with the following fields: id, name, created_at, updated_at, deleted_at.

Working with dates in the model

Eloquent assumes by default that the created_at and updated_at fields will be present in your model. If you did not create them in the migration, then the support for these fields in the model will need to be disabled by adding the following line to the model.

public $timestamps = false;

And if you plan to work with “soft deletion” of data from your table. That is, your data will not be deleted, but the deleted_at column (which by default contains Null) will contain the deletion date (such rows will not be displayed by Eloquent). To do this, add the Illuminate\Database\Eloquent\SoftDeletes trait to your model.

In the model below, support for standard create and update fields is disabled, but soft delete is enabled.

<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Person extends Model
{
    use SoftDeletes;
    public $timestamps = false;
}

If your creation and update columns in the database are named somehow differently, then you can customize them by adding constants to the model, so Laravel will understand what these columns are.

const CREATED_AT = 'create';
const UPDATED_AT = 'update';

Dates in Laravel models

Now you can create a new record in the database in Laravel as follows.

    $person = new Person;
    $person->name = 'Your name';
    $person->save();

Columns created_at and updated_at will automatically update with the current date.

When edited, only the updated_at field will be updated.

$person = Person::find(1);
$person->name = 'Your name';
$person->save();

You can also update this field to the current time without changing the data, for this you can call the touch method.

$person->touch();

You can also use the previously created fields to sort the output.

$persons = Person::orderBy('created_at', 'desc')->get();

Also Laravel converts timestamp columns to Carbon object, so you can manipulate them accordingly. For example, adding 10 days to a date would look like this.

$person->created_at->addDays(10);

I hope today’s article about working with dates in Laravel was useful to you and you have a better understanding of how the Eloquent ORM works. I always publish announcements of new articles on my Twitter, subscribe to be the first to know about it.

Share post
Twitter Facebook