How to Change Table Structures With Migration in Laravel?

On Previous blog we learned how to perform CRUD operations with Laravel Framework. Now that we have our database, we will look another aspect of Backend. We will explore how to make changes in Table Structures. This is the table structure I have used in my previous blog to perform the Crud Operation. This code snippet below is a migration script.

    public function up()
        Schema::create('students', function (Blueprint $table) {

If you are running your application in backend than you can simply use this sql query and add a new column:

alter table students add column subject varchar(255);

If you have to make change in your production server than you need to create another migration script manually and add your sql query there. So the initial step is to create a migration table from terminal with following command:

php artisan make:migration add_subject_column_to_students_table --table=students

With this command you will see a migration table created inside migration folder. Open the file and add your script to add the column username. Code is given below:

public function up()
    Schema::table('students', function (Blueprint $table) {

After that you need to do php artisan migrate. It will finally make your changes in database and add column subject.

I found out that Laravel doesn’t automatically create migration of our scripts changes like that of Entity Framework and Django Framework. All of our changes in the database schema needs to be done either from MySQL queries of manually in the migration files.


Comments are closed.