Skip to main content

Laravel Migration (Update column, dropColumn) simplest and easiest way!

↬HOW TO UPDATE/DROP COLUMN WITHOUT LOOSING DATA? 



LARAVEL MIGRATION: UPDATING A COLUMN WITHOUT LOOSING DATA: 

Example:
Suppose i have a database table which has this fields:

id
slider_title
slider_description

and the migration of that table looks like this:


Schema::create('slide_cases', function (Blueprint $table) {
  $table->bigIncrements('id');
  $table->string('slider_title')->nullable();
  $table->string('slider_description')->nullable();
  $table->timestamps();
});

Now, if i want to change the slide_description field data type from string to longText using laravel migration, simply do the following steps: 

1) Open your command prompt and type:


     php artisan make:migration update_slider_description_in_slide_cases --table=slide_cases


➽PLEASE KEEP IN  MIND that slide_cases is my table name; so you have to give your table name in that place :)

2) Now, in your newly created migration file for updating slider_description do the followings:

Schema::table('slide_cases', function (Blueprint $table) {
  $table->longText('slider_description')->nullable()->change();
});

➽PLEASE KEEP IN  MIND that while updating in laravel migration you have to give change() at last :)

3) Finally you are ready to migrate now! ;)
    In your command prompt just type:
    
                php artisan migrate

I hope you have finally solved the problem :D Let me know your feedback.


LARAVEL MIGRATION: DROP/DELETE A COLUMN WITHOUT LOOSING DATA: 

Example:
Suppose i have a database table which has this fields:

id
member_name
member_address



and the migration of that table looks like this:


Schema::create('team_members', function (Blueprint $table) {
  $table->bigIncrements('id');
  $table->string('member_name');
  $table->string('member_address');
  $table->timestamps();
});

Now, if i want to drop the member_address field data using laravel migration, simply do the following steps: 

1) Open your command prompt and type:


php artisan make:migration drop_member_address_in_team_members --table=team_members


➽PLEASE KEEP IN  MIND that team_members is my table name; so you have to give your table name in that place :)

2) Now, in your newly created migration file for dropping member_address do the followings:

Schema::table('team_members', function (Blueprint $table) {
             $table->dropColumn('designation');
        });

 ➽PLEASE KEEP IN  MIND that while dropping column in laravel migration you have to write dropColumn() :)

3) Finally you are ready to migrate now! ;)
    In your command prompt just type:
    
                php artisan migrate

I hope you have finally solved the problem :D Let me know your feedback.

Comments

Popular posts from this blog

Laravel Vuejs Basic Setup

In this article we will see how to setup vue with laravel. 1) Create a laravel project: laravel new laravue 2) In web.php create a temporary route:  Route :: get ( '/test' ,  function  () {      return   view ( 'welcome' ); }); So, when you will see the welcome page once you hit the  http://laravue.test/test  url. 2) In terminal run:  npm install 3) npm install vue (In package.json file you will see that vue dependency has been added successfully) 4) Now go to resources -> js -> app.js file. Here we will have to load the vue. write the following lines in app.js.  require ( './bootstrap' ); window . Vue  =  require ( 'vue' ); const   app  =  new   Vue ({      el :   '#app' }); (Here, vue js will execute and control anything that will be inside the app id. So, for our example  let's put the app id inside the welcome view). So, inside welcome.blade.php 's body...