Bird
0
0

You want to create a migration that renames the username column to user_name in the users table. Which code snippet is correct?

hard📝 Application Q9 of 15
Laravel - Database Basics and Migrations
You want to create a migration that renames the username column to user_name in the users table. Which code snippet is correct?
ASchema::renameColumn('users', 'username', 'user_name');
BSchema::table('users', function (Blueprint $table) { $table->renameColumn('username', 'user_name'); });
CSchema::table('users', function (Blueprint $table) { $table->string('user_name'); $table->dropColumn('username'); });
DSchema::table('users', function (Blueprint $table) { $table->changeColumn('username', 'user_name'); });
Step-by-Step Solution
Solution:
  1. Step 1: Identify the correct method to rename columns

    Laravel provides renameColumn method on the Blueprint object inside Schema::table.
  2. Step 2: Check other options for validity

    Schema::renameColumn does not exist, dropping and adding columns is not rename, and changeColumn is invalid.
  3. Final Answer:

    Schema::table('users', function (Blueprint $table) { $table->renameColumn('username', 'user_name'); }); -> Option B
  4. Quick Check:

    Rename column = $table->renameColumn() [OK]
Quick Trick: Use $table->renameColumn() inside Schema::table [OK]
Common Mistakes:
  • Trying to rename columns with Schema::renameColumn
  • Dropping and adding columns instead of renaming
  • Using non-existent changeColumn method

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Laravel Quizzes