0

Can anybody advise me, in great detail, on how to go about extending Illuminate\Database\Schema so as to add a zerofill function and also modify the current integer function to take a length argument, all while not actually modifying any of the vendor files.

Thank you kindly.

EDIT: or is there a way to add a column manually with the Schema Builder, that I'm just missing cause of my heavy Friday afternoon head?

EDIT: went with the approach as suggested by Gareth Oakley by adding a DB::statement() call after Schema::create():

<?php

    class CreateExamplesTable extends Migration {

        public function up()
        {
            Schema::create('examples', function(Blueprint $table)
            {
                $table->engine = 'InnoDB';

                $table->increments('id')->unsigned();
                $table->integer('account_id')->unsigned();
                $table->string('card_id', 20)->nullable();
                $table->boolean('active')->default(true);
                $table->timestamps();

                $table->foreign('account_id')->references('id')->on('accounts')->onDelete('cascade')->onUpdate('cascade');
            });

            DB::statement('ALTER TABLE examples ADD invoice_id INT(6) UNSIGNED ZEROFILL NOT NULL AFTER card_id');
        }

        public function down()
        {
            Schema::drop('examples');
        }
    }
Antonio Carlos Ribeiro
  • 86,191
  • 22
  • 213
  • 204
Yasha Nisanov
  • 178
  • 3
  • 10
  • Laravel does not provide such feature. You can pick one of smallinteger/integer/biginteger types - get one that fits the best. Personaly I would not use zerofill on database side. I would format the output on application side. – Andreyco Jul 26 '13 at 11:10
  • I've posted an answer below - but agreed. I'd generally leave anything related to how data is presented outside of the database and put it in my views instead. You could use `str_pad` - http://php.net/manual/en/function.str-pad.php – Gareth Oakley Jul 26 '13 at 11:29

1 Answers1

2

I've not needed to use zerofills before - I'm guessing you're referring to this functionality:

When used in conjunction with the optional (nonstandard) attribute ZEROFILL, the default padding of spaces is replaced with zeros. For example, for a column declared as INT(4) ZEROFILL, a value of 5 is retrieved as 0005.

Given that it's not a standard SQL attribute you'll probably have to create the column manually using the DB class:

DB::statement('ALTER TABLE MyTable ADD MyColumn INT UNSIGNED ZEROFILL NOT NULL');

The schema build is great for the vast majority of cases where you don't need to use database specific features but it looks like others have occasionally had to do similar things:

Make column not nullable in a Laravel migration

Community
  • 1
  • 1
Gareth Oakley
  • 1,020
  • 1
  • 12
  • 34
  • Even though this is not actually what I really want to do; it's still the simplest more common sense implementation so thank you very much. – Yasha Nisanov Jul 28 '13 at 21:54