Raheel Hasan
Raheel Hasan

Reputation: 6023

Laravel5: How to work with the timestamp field in migration

Im trying to put a table via migration and here is what im stuck at:

I need the following:

$table->timestamps('added_on');

and NOT the created_at and updated_at which are forced into the table even though I didnt mention them in the schema.

Upvotes: 1

Views: 2695

Answers (2)

Poiz
Poiz

Reputation: 7617

To add a Unique Timestamp of your own making; all you need do is call the $table->timestamp()Method passing it your field_name: in your case added_on. So, now; assuming you have a Migration called "TestTable". Here below would be what your Migration File may look like:

<?php


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

    class TestTable extends Migration {


        public function up() {
            // Create table for storing roles
            Schema::create('table_name', function (Blueprint $table) {
                $table->increments('id');
                $table->string('field_1')->unique();
                $table->string('field_2');
                $table->string('field_3');

                // THE COMMENTED LINE BELOW ADDS THE 
                // "created_at" & "updated_at" FIELDS IN DB
                // $table->timestamps();        //<== ADDS GENERIC TIMESTAMPS

                // NOW TO ADD YOUR OWN UNIQUE DATE-BASED/TIMESTAMP FIELD,
                // ALL YOU NEED DO IS:::
                $table->timestamp("added_on");  //<== ADD YOUR DATE/TIME FIELD

            });
        }
    }

Upvotes: 0

thefallen
thefallen

Reputation: 9749

The timestamps() method is adding the created_at and updated_at columns. What you need is timestamp() (without the s) which accepts as an argument a timestamp column name.

$table->timestamp('added_on');

Upvotes: 6

Related Questions