28 February 2014
Once you database tables created, it is a complex task to update these tables names and their structure. But i think laravel has another opinion .Let’s get started and i will show you how laravel take care of you.
The first thing we need is to change table name. Here’s an example.
Schema::create('clients',function($table){
//add clients table structure
$table->integer('id')->primary();
});
//change clients table name to customers
Schema::rename('clients','customers');
Also if you like to drop table ,You can use dropIfExists()
like that.
//drop customers table if exists
Schema::dropIfExists('customers');
If you like to drop column .You can provide column name to dropColumn()
method .This method accepts array if you like to drop many columns. Here’s an example.
Schema::table('clients',function($table){
//drop name column
$table->dropColumn('name');
//drop username,email and pwd columns
$table->dropColumn(array('username','email','pwd'));
});
We can simply rename any column with renameColumn()
method. Here’s an example.
Schema::table('clients',function($table){
//rename name column to username
$table->renameColumn('name','usename');
});
Do you remember primary keys that we explored in part1 of this series. Here’s simple example of primary keys.
Schema::create('clients',function($table){
//add clients table structure
$table->integer('id')->primary();
});
Now we can drop primary attribute of id
column using dropPrimary()
method like that.
Schema::table('clients',function($table){
$table->dropPrimary('id');
});
dropUnique()
and dropIndex()
is same as dropPrimary()
except that they accept single parameter, which consists of table name ,column name and unique or index separated by underscores . first let’s create table with columns holding these attributes.
Schema::create('clients',function($table){
//add clients table structure
$table->integer('id')->index();
$table->string('email',150)->unique();
});
Here’s how to remove these attributes.
Schema::table('clients',function($table){
//drop unique attribute from email column
$table->dropUnique('clients_email_unique');
//drop index attribute from id coulmn
$table->dropIndex('clients_id_index');
});