Can I Only Update One Column in Tradegecko for Uploads
M H Hasib
Posted on • Updated on
Update existing tabular array's column with migration without losing data in Laravel
Before going to start our coding we need to know about Laravel Migration and how information technology helps us.
Showtime of all, if we desire to build a project. We demand to blueprint our database kickoff. Then what tin nosotros practise? We can pattern our database directly. If you lot miss some field/column or some datatype, yous need to fix information technology directly into the database which is a bad practice in Laravel.
In another scenario, Suppose you have a project and it needs to set upwardly to your friend's device, y'all might need to send the SQL file; which is painful.
Now think if you lot accept the feature like you lot pull the project and run a command, Boom your database is being ready. Isn't information technology very helpful?
Laravel migration does the same. Y'all don't demand to take the hurting of SQL file or if you miss a information type or any spelling mistake, yous tin fix it through your coding. You lot need not work directly with the database. You will prepare it to your code and just run php artisan migrate:refresh. So hopefully you lot got the benefit of Laravel migration.
At present let's jump to another scenario.
Sometimes we are much worried when we need to add together or change our existing database table.
For example, we take a User Model and users migration table and information technology contains name, username, email_verified_at password.
<?php use Illuminate\Database\Migrations\Migration ; use Illuminate\Database\Schema\Blueprint ; employ Illuminate\Support\Facades\Schema ; class CreateUsersTable extends Migration { /** * Run the migrations. * * @render void */ public role up () { Schema :: create ( 'users' , function ( Design $tabular array ) { $table -> bigIncrements ( 'id' ); $table -> string ( 'name' ); $table -> string ( 'email' ) -> unique (); $table -> timestamp ( 'email_verified_at' ) -> nullable (); $table -> string ( 'password' ); $table -> rememberToken (); $table -> timestamps (); }); } /** * Reverse the migrations. * * @return void */ public part downwards () { Schema :: dropIfExists ( 'users' ); } }
But after deploying our project and afterward having our many users we establish, we need to add a new field in our table like phone_number. Besides mentioned, we need to continue our existing data.
Then, nosotros will create a new migration file for adding a new cavalcade in our existing table.
At present use this command:
php artisan make:migration add_phone_number_to_users_table
Here we need to ensure that the table'southward name needs to match the new migration file like we use users in both places.
After using the control we can encounter a new migration file like this:
<?php use Illuminate\Database\Migrations\Migration ; utilize Illuminate\Database\Schema\Blueprint ; apply Illuminate\Back up\Facades\Schema ; class AddPhoneNumberToUsersTable extends Migration { /** * Run the migrations. * * @render void */ public function up () { Schema :: table ( 'users' , function ( Blueprint $table ) { // }); } /** * Opposite the migrations. * * @return void */ public office downward () { Schema :: table ( 'users' , function ( Blueprint $table ) { // }); } }
Now you tin can add your want field in this migration file. As we demand a phone_number field.
<?php apply Illuminate\Database\Migrations\Migration ; use Illuminate\Database\Schema\Pattern ; use Illuminate\Support\Facades\Schema ; class AddPhoneNumberToUsersTable extends Migration { /** * Run the migrations. * * @return void */ public function upwards () { Schema :: tabular array ( 'users' , role ( Blueprint $table ) { $tabular array -> cord ( 'phone_number' ) -> unique () -> after ( 'email' ); }); } /** * Reverse the migrations. * * @render void */ public function down () { Schema :: table ( 'users' , function ( Blueprint $table ) { // }); } }
Here nosotros added phone_number field after email in up() method. You have the flexibility to put that cavalcade where ever you want past using later on()
or before()
. You lot can learn more than modifier from Laravel Documentation Column Modifier
Now we are one step backside from our task done.
<?php use Illuminate\Database\Migrations\Migration ; use Illuminate\Database\Schema\Blueprint ; utilize Illuminate\Support\Facades\Schema ; course AddPhoneNumberToUsersTable extends Migration { /** * Run the migrations. * * @return void */ public function up () { Schema :: table ( 'users' , function ( Blueprint $tabular array ) { $tabular array -> string ( 'phone_number' ) -> unique () -> later ( 'email' ); }); } /** * Reverse the migrations. * * @return void */ public function down () { Schema :: table ( 'users' , function ( Design $table ) { $table -> dropColumn ( 'phone_number' ); }); } }
Hither you lot can encounter that nosotros are adding $table->dropColumn('phone_number');
so that if nosotros run the rollback command and information technology will work properly. If you take many filed you lot can use assortment of dropcolumn like this $table->dropColumn(['phone_number']);
And finally, here we go...
Now you need to run this control:
php artisan migrate
And we accept washed our job. :D
You tin visit Laravel Migration Documentation for more migration options.
Hope it will help you lot.
Source: https://dev.to/mahmudulhsn/update-existing-table-with-migration-without-losing-in-data-in-laravel-fb1
0 Response to "Can I Only Update One Column in Tradegecko for Uploads"
Enregistrer un commentaire