Skip to main content

How to Selectively Disable Timestamp Columns in Laravel Models

Introduction:

In Laravel, the updated_at and created_at columns are timestamp columns that automatically update whenever a model is saved or created. However, there might be scenarios where you want to selectively disable one or both of these columns for specific models. Fortunately, Laravel provides a straightforward way to achieve this by customizing your model's const UPDATED_AT and const CREATED_AT constants. In this blog post, we will explore how to selectively disable timestamp columns in Laravel models, discuss the benefits of this approach, and guide you through the necessary steps.


Step 1: Create a Laravel Model and Migration

To demonstrate this process, let's assume we have a model called Download that represents a downloadable file in our application. If you don't have a model already, you can create one using the php artisan make:model Download command in your terminal.

To generate the migration for the downloads table, run the following command: php artisan make:migration create_downloads_table. This will create a new migration file in the database/migrations directory.


Step 2: Edit the Migration File

Open the newly created migration file (database/migrations/xxxxxxxx_create_downloads_table.php) and locate the up() method. By default, Laravel includes the timestamps() method in the create blueprint, which adds created_at and updated_at columns to the table. However, you can modify the migration file accordingly if you want to selectively disable one or both of these columns.

To disable the updated_at column, remove the ->timestamps() line from the migration file. If you also want to disable the created_at column, remove the ->timestamps() line and add a ->timestamp('created_at')->nullable() line to the migration file.

Your updated migration file should look something like this:

<?php

use Illuminate\Database\Migrations\Migration;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Support\Facades\Schema;


class CreateDownloadsTable extends Migration

{

    public function up()

    {

        Schema::create('downloads', function (Blueprint $table) {

            $table->id();

            $table->string('file_name');

            // remove this line
            // $table->timestamps();

            // Add this line if you want to keep created_at column

            // $table->timestamp('created_at')->nullable();


            // Add this line if you want to keep updated_at column

            // $table->timestamp('updated_at')->nullable();


            // Other columns if needed

        });

    }

    // Rest of the migration file

}


Step 3: Customize Timestamp Columns in the Model

Open the Download model file (app/Models/Download.php) and customize the const UPDATED_AT and const CREATED_AT constants based on your requirements.

To disable the updated_at column, add the following line within the class definition:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;


class Download extends Model

{

    const UPDATED_AT = null;

    // Other model code

}

If you also want to disable the created_at column, add the following line as well:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;


class Download extends Model

{

    const UPDATED_AT = null;

    const CREATED_AT = null;

    // Other model code

}

By customizing these constants, Laravel will no longer manage the values of the corresponding timestamp columns when saving or creating instances of the Download model.


Step 4: Run the Migration

To apply the changes to the database, run the migration using the php artisan migrate command. This will create the downloads table with the selectively disabled timestamp columns.


Conclusion:

Selective disabling of timestamp columns in Laravel models allows you to have fine-grained control over their behaviour. By customizing the const UPDATED_AT and const CREATED_AT constants in your model and modifying the migration file accordingly, you can disable one or both of these columns for specific models. This approach is useful in scenarios where you want to exclude certain models from automatic timestamp updates or when you have custom timestamp handling requirements. Remember to consider the specific needs of your project and apply this approach selectively to maintain the integrity of your data and optimize performance.

Comments

Popular Articles

Installing Docker on Ubuntu 18.04 ([SOLVED]- libseccomp2 version 2.3.1 installed but 2.4 required)

 Introduction: If you're attempting to install Docker on Ubuntu 18.04 Bionic Beaver, which is known for having installation issues, this comprehensive guide aims to address common errors you may encounter during the process. Our goal is to save you time searching for answers on StackOverflow by providing clear instructions. The guide is based on the official Docker documentation available at https://docs.docker.com/engine/install/ubuntu/#install-using-the-repository. Step 1: Updating Package Lists To begin, run the following command to update your package lists using the "apt-get update" command: sudo apt-get update Note: If you encounter any errors during the update process, it may be due to the older Ubuntu 18.04 release. In such cases, you can refer to the Configure DNS server IP to resolve errors with apt-get update  guide for assistance. This guide will help you address any DNS-related issues that might be preventing successful updates. Step 2: Installing Dependencie...

[SOLVED] Linux - Issues installing Docker on Ubuntu - libseccomp2 version 2.3.1 installed but 2.4 required

This article has been improved for a better understanding - goto  https://splashcoder.blogspot.com/2023/07/installing-docker-on-ubuntu-1804-solved.html There is a possibility that you are trying to install docker. There is a very comprehensive guide at https://docs.docker.com/engine/install/ubuntu/#install-using-the-repository. The linked guide serves as a reference for this article. We try to address the common errors in this article. Just so you won’t have to scour the entire answers on StackOverflow. Step 1: The first thing is to run our famous "apt update" command. So run the command below. sudo apt-get update You may observe that there are some errors. And YES! we are using a fairly old Ubuntu 18.04 Bionic Beaver Release. It seems perfect for this as most people have issues installing docker anyways. To resolve this, you may refer to  Configure DNS server IP to resolve errors with apt-get update Step 2: Following the Docker article, we should run the commands below. sudo...

Issue with installing python-axolotl or python-axolotl-curve25519 on Windows

It is peeve amazing how many hours get burnt while trying to resolve simple package dependency issues. I may be too dumb to proffer a lasting one-fits-all solution to all dependency issues, but I got this issue fixed in my own case. Problem Description: The python-axolotl library requires certain dependencies that are not properly managed when installing libraries via a command line's requirement.txt file. I got real help by reading through this link below, but this page might be more helpful when it comes to detailed instructions. Actual instructions: https://github.com/tgalal/python-axolotl Typical errors might look like the following: 1. In this case, missing library(s). Libraries needed for compiling some python resources. Unfortunately, http://aka.ms/vcpython27  has been decommissioned because Python2x is EOL. You can still get the VcPython27 from an archive at https://web.archive.org/web/20210106040222/download.microsoft.com/download/7/9/6/796EF2E4-801B-4F...