Mubasheer Shaik
May 28, 2019 · 2 min read

Create a Laravel migration to update/add fields to the table

In this example, we have to add an extra six fields in the “users” table.

Extra fields:

  1. first name
  2. last name
  3. set address
  4. city
  5. state
  6. zip

Create migration:

create a new migration to add fields to the users table.

php artisan make:migration add_fields_to_users

A new migration has been created under database/migrations folder in your application:

Now time to add extra fields in the migration file. Open the migration file and add the following code inside the public function up() method

public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('Firstname');
$table->string('Lastname');
$table->string('setaddress');
$table->string('city');
$table->string('state');
$table->string('zipcode');
});
public function down()
{
Schema::table(‘contacts’, function (Blueprint $table) {
$table->dropColumn(“Firstname”);
$table->dropColumn(“Lastname”);
$table->dropColumn(“setaddress”);$
table->dropColumn(“city”);
$table->dropColumn(“state”);
$table->dropColumn(“zipcode”);
});

Finally, execute the below command in the terminal:

php artisan migrate

All the extra six fields we specified is added in the “users” table.

Hope this helps! Happy coding!!

Mubasheer Shaik

Written by

Tech lead, GKB LABS.

More From Medium

More from Mubasheer Shaik

Top on Medium

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade