Insert the current date time using Laravel

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Effortlessly Insert Current Date Time in Your Laravel App Using Eloquent Models

One of the essential functionalities of any application is to store the current date and time when a record is created or updated. In Laravel, you can achieve this using a combination of Eloquent models and built-in helper functions. This blog post will explain the process in detail and provide examples to make it easy for you to implement in your projects.

Inserting Current Date Time Using Eloquent Models

Eloquent is Laravel's Object-Relational Mapping (ORM) library, which provides a convenient interface for working with database tables. To store the current date and time in your application, you can use Eloquent models to manage data persistence. Here's an updated example of inserting data with timestamps:

function insert(Request $req)
{
    $name = $req->input('name');
    $address = $req->input('address');
    // Instantiate Eloquent model and set relevant fields with values
    $user = new User();
    $user->name = $name;
    $user->address = $address;
    // Set the timestamps to be automatically populated with current date and time
    $user->setTimestamps();
    // Save the user record to the database
    $user->save();
    echo "Record inserted successfully.<br/>";
    return redirect('/');
}

In this code block, we have instantiated a new User model and set its attributes - name and address - to the values provided by the request. Then, by calling setTimestamps(), we enable automatic timestamp generation for the created_at and updated_at columns when saving the record in the database. You can customize this functionality as per your application's needs.

Customizing Timestamps Behavior

Laravel provides flexibility to modify timestamps behavior through various parameters such as date format, timezone, and auto-incremental values. For example:

function insert(Request $req)
{
    $name = $req->input('name');
    $address = $req->input('address');
    // Instantiate Eloquent model and set relevant fields with values
    $user = new User();
    $user->name = $name;
    $user->address = $address;
    // Create a custom timestamp format using Carbon library (install 'laravel/carbon' package)
    $datetimeFormat = 'Ymd-Hi:s';
    $customTimestamp = Carbon::now()->format($datetimeFormat);
    $user->created_at = $customTimestamp;
    // Set the timestamps to be automatically populated with current date and time
    $user->setTimestamps();
    // Save the user record to the database
    $user->save();
    echo "Record inserted successfully.<br/>";
    return redirect('/');
}

In this code, we have created a custom timestamp format using Carbon library and set a specific date and time for the created_at column. Upon saving the record in the database, Laravel will use this format for the timestamps.

Conclusion

Inserting current date and time in your Laravel application using Eloquent models is straightforward. By understanding the available options for timestamping records, you can make informed decisions about how to handle this essential functionality. For more detailed information on working with timestamps, refer to the official Laravel documentation.

Managing Timestamps in Laravel Applications