Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Timmypro's avatar

Migration and Column Type

I'm trying to include an additional dateTime field to my products table with the name "expiry_date". I will be using this date in the future to check for product's expiry date. I don't know the best column type to assign for this field, also, I'm thinking of using Carbon but I don't really know the best approach to implement such.

Products Migration

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateProductsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            $table->increments('id');
            $table->string('prod_name');
            $table->string('barcode');
            $table->string('model')->nullable();
            $table->bigInteger('quantity');
            $table->decimal('amount', 8, 2);
            $table->decimal('total_amount', 8, 8);
            $table->integer('manufacturer_id');
            $table->integer('batch_id');

            $table->foreign('manufacturer_id')->references('id')->on('manufacturers')
                ->onDelete('cascade')->onUpdate('cascade');

            $table->foreign('batch_id')->references('id')->on('batches')
                ->onDelete('cascade')->onUpdate('cascade');

            $table->dateTime('expiry_date');

            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('products');
    }
}

0 likes
1 reply
Sti3bas's avatar
Sti3bas
Best Answer
Level 53

I don't know the best column type to assign for this field

Laravel uses timestamp type for created_at and updated_at timestamps, so I guess you should do the same.

https://github.com/laravel/framework/blob/6.x/src/Illuminate/Database/Schema/Blueprint.php#L992

I'm thinking of using Carbon but I don't really know the best approach to implement such

You just need to add this to your model class:

protected $casts = [
    'expiry_date' => 'datetime',
];

Then $product->expiry_date will return Carbon instance.

https://laravel.com/docs/6.x/eloquent-mutators#date-casting

Please or to participate in this conversation.