Laravel Schema

Indicate that the table needs to be created

Schema::create('table', function($table) { 
    $table->increments('id'); 
});

Specify a Connection

Schema::connection('foo')->create('table', function($table){});

Rename the table to a given name

Schema::rename($from, $to);

Indicate that the table should be dropped

Schema::drop('table');

Indicate that the table should be dropped if it exists

Schema::dropIfExists('table');

Determine if the given table exists

Schema::hasTable('table');

Determine if the given table has a given column

Schema::hasColumn('table', 'column');

Update an existing table

Schema::table('table', function($table){});

Indicate that the given columns should be renamed

$table->renameColumn('from', 'to');

Indicate that the given columns should be dropped

$table->dropColumn(string|array);

The storage engine that should be used for the table

$table->engine = 'InnoDB';

Only work on MySQL

$table->string('name')->after('email');

Laravel Indexes

$table->string('column')->unique();
$table->primary('column');

Creates a dual primary key

$table->primary(array('first', 'last'));
$table->unique('column');
$table->unique('column', 'key_name');

Creates a dual unique index

$table->unique(array('first', 'last'));
$table->unique(array('first', 'last'), 'key_name');
$table->index('column');
$table->index('column', 'key_name');

Creates a dual index

$table->index(array('first', 'last'));
$table->index(array('first', 'last'), 'key_name');
$table->dropPrimary('table_column_primary');
$table->dropUnique('table_column_unique');
$table->dropIndex('table_column_index');

Laravel Foreign Keys

$table->foreign('user_id')->references('id')->on('users');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'|'restrict'|'set null'|'no action');
$table->foreign('user_id')->references('id')->on('users')->onUpdate('cascade'|'restrict'|'set null'|'no action');
$table->dropForeign('posts_user_id_foreign');

Laravel Column Types

Increments

$table->increments('id');
$table->bigIncrements('id');

Numbers

$table->integer('votes');
$table->tinyInteger('votes');
$table->smallInteger('votes');
$table->mediumInteger('votes');
$table->bigInteger('votes');
$table->float('amount');
$table->double('column', 15, 8);
$table->decimal('amount', 5, 2);

String and Text

$table->char('name', 4);
$table->string('email');
$table->string('name', 100);
$table->text('description');
$table->mediumText('description');
$table->longText('description');

Date and Time

$table->date('created_at');
$table->dateTime('created_at');
$table->time('sunrise');
$table->timestamp('added_on');

Adds created_at and updated_at columns

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

Others

$table->binary('data');
$table->boolean('confirmed');

Adds deleted_at column for soft deletes

$table->softDeletes();
$table->enum('choices', array('foo', 'bar'));

Adds remember_token as VARCHAR(100) NULL

$table->rememberToken();

Adds INTEGER parent_id and STRING parent_type

$table->morphs('parent');
->nullable()
->default($value)
->unsigned()

Leave a Reply

Your email address will not be published.