Cygnite - A Modern Toolkit For Web Developers

The Elegant Way of Building Full-Featured Web Applications

Cygnite Dynamic Routing

Schema Builder


Schema Builder


Cygnite provides you a simple and flexible way to build your database scheme. Currently schema builder supports widely used open source database mysql. Schema builder reduce all your pain of writing raw queries. Cygnite Schema builder is also used for Database Migration.

Create a Table

Closure object is used to create a new database table. Schema will create a Table if doesn't exists.


    use Cygnite\Database\Table\Schema; 

    Schema::make('users', function ($table)
        //$table->on("cyrus");// database connection
                ['column'=> 'id', 'type' => 'int', 'length' => 11,
                    'increment' => true, 'key' => 'primary'],
                ['column'=> 'username', 'type' => 'string', 'length' =>100],
                ['column'=> 'password', 'type' => 'string', 'length'  =>16],
                ['column'=> 'country', 'type' => 'string', 'length'  =>20],
                ['column'=> 'city', 'type' => 'string', 'length'  =>16, 'null'=> true],
                ['column'=> 'is_admin', 'type' => 'enum', 'length'  =>['0', '1']],
                ['column'=> 'price', 'type' => 'decimal', 'length'  =>'10,2'],
                ['column'=> 'depth', 'type' => 'float', 'length'  =>'10,2'],
                ['column'=> 'created_at', 'type' => 'datetime'],
                ['column'=> 'updated_at', 'type' => 'datetime'],
                ['column'=> 'access_time', 'type' => 'time'],
                ['column'=> 'time', 'type' => 'timestamp',
            ], 'InnoDB', 'latin1'

[Note: If you running your application in higher version of MYSQL (example: MYSQL 5.7.9) you need to specify the default value for created_at, updated_at fields in length value.]
 //Your schema to migrate
        Schema::make('user', function ($table)
                    ['column'=> 'created_at', 
                     'type' => 'datetime', 'length' => 'NOT NULL DEFAULT NOW()'],
                    ['column'=> 'updated_at', 'type' => 'datetime', 
                     'length' => 'DEFAULT NOW()'],

            ], 'InnoDB', 'latin1');

Specifying the Connection

You may wish to create schema for different database instead of default database set in your configuration. In that case you need to specify the connection name as below.

  Schema::make('user', function ($table)
      //Your database connection name

           ['column'=> 'created_at', 
               'type' => 'datetime', 'length' => 'NOT NULL DEFAULT NOW()'],
           ], 'InnoDB', 'latin1');

Has Table

 if ($table->hasTable()) {
     echo "Table exists";

Column Exists


Renaming Table

Rename your table with below command.


Add A Column To The Table

    $table->addColumn('features', 'varchar(100) NOT NULL');

Add Column After

    $table->addColumn('description', 'varchar(100) NOT NULL')

Dropping Single Column


Drop Multiple Columns


Adding Primary Key


Add Multiple Column As Primary Key

    $table->addPrimaryKey(['id', 'some']);

Adding Unique Key


Add Multiple Columns As Unique Key

   $table->unique(['depth','rise'], 'users');

Dropping Unique Key


Add And Drop Index Key


Drop Table

Drop your table from the database if exists with below command.

if ($table->drop('users')) {
    echo "Table dropped";

This is all about the Schema Builder. Simply run and enjoy building the table schema. You will find much more features in the next releases. Stay connected.

Follow Us On Facebook Twitter Google+ Linkedin
Released Under The MIT Public License. Copyrights @2012-2017. Powered by- Sanjoy Dey Productions.