Cygnite - A Modern Toolkit For Web Developers

The Elegant Way of Building Full-Featured Web Applications

Cygnite Dynamic Routing

Schema Builder

Documentation

Schema Builder

Introduction

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
        $table->create(
            [
                ['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',
                    'length'=> "DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_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)
        {
            $table->create([
                    ['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
      $table->on('cyrus');

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


Has Table


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

Column Exists


   $table->hasColumn('email');

Renaming Table

Rename your table with below command.


    $table->rename('users_category');

Add A Column To The Table


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

Add Column After


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

Dropping Single Column


   $table->dropColumn('features');

Drop Multiple Columns


    $table->dropColumn([
               'features',
               'user_detail',
               'status']);

Adding Primary Key


     $table->addPrimary('id');

Add Multiple Column As Primary Key


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

Adding Unique Key


    $table->unique('sunrise');

Add Multiple Columns As Unique Key


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

Dropping Unique Key


     $table->dropUnique('users');

Add And Drop Index Key


    $table->index('is_admin');
    $table->dropIndex('is_admin');

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.