Bird
0
0

You want to create a migration that adds a nullable 'bio' text column to the existing 'users' table. Which up() method code is correct?

hard📝 state output Q15 of 15
Laravel - Database Basics and Migrations
You want to create a migration that adds a nullable 'bio' text column to the existing 'users' table. Which up() method code is correct?
ASchema::table('users', function (Blueprint $table) { $table->text('bio')->nullable(); });
BSchema::create('users', function (Blueprint $table) { $table->text('bio')->nullable(); });
CSchema::modify('users', function (Blueprint $table) { $table->text('bio')->nullable(); });
DSchema::addColumn('users', 'bio', 'text')->nullable();
Step-by-Step Solution
Solution:
  1. Step 1: Understand how to modify existing tables

    To add a column to an existing table, use Schema::table with a closure defining the new column.
  2. Step 2: Evaluate each option

    Schema::table('users', function (Blueprint $table) { $table->text('bio')->nullable(); }); correctly uses Schema::table and adds a nullable text column. Schema::create('users', function (Blueprint $table) { $table->text('bio')->nullable(); }); tries to create a new table, which is wrong. Options A and D use invalid Schema methods.
  3. Final Answer:

    Schema::table('users', function (Blueprint $table) { $table->text('bio')->nullable(); }); -> Option A
  4. Quick Check:

    Use Schema::table to add columns [OK]
Quick Trick: Use Schema::table to change existing tables [OK]
Common Mistakes:
  • Using Schema::create instead of Schema::table
  • Using non-existent Schema methods
  • Forgetting nullable() for optional columns

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Laravel Quizzes