Laravel Migration Columns
A migration column is one field in a table. Each column has a name and a type.
Short and long text
A title is short text and a summary is long text.
$table->string('title');
$table->text('summary');Numbers and true or false
Pages is a whole number and published is true or false.
$table->integer('pages');
$table->boolean('published');All the columns
The books table holds an id, a title, a summary, a page count and a published flag.
$table->id();
$table->string('title');
$table->text('summary');
$table->integer('pages');
$table->boolean('published');Example
return new class extends Migration
{
public function up(): void
{
Schema::create('books', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('summary');
$table->integer('pages');
$table->boolean('published');
$table->timestamps();
});
}
}Key points
- Use string() for short text, such as a title.
- Use text() for long text, such as a summary.
- Use integer() for whole numbers, such as a page count.
- Use boolean() for a true or false value.
- Use id() for the table's auto-numbered id.
In this example, the up method builds the books table. It adds one column for each field of a book. The timestamps record the creation and change times. A migration column is one field in a table.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
Which column type holds a whole number?