Advertisement

Google Ad Slot: content-top

Laravel Creating Tables


Create Table Migration

Use this command to generate a migration for creating a table:

php artisan make:migration create_posts_table


This generates a file in database/migrations/:

public function up(): void
{
  Schema::create('posts', function (Blueprint $table) {
    $table->id();
    $table->string('title');
    $table->text('content');
    $table->timestamps();
  });
}

public function down(): void
{
  Schema::dropIfExists('posts');
}


Running Migrations:

Run all pending migrations and create tables

php artisan migrate
Output:
Name Type
id bigint(20)
title varchar(255)
content text
created_at timestamp
updated_at timestamp

Rollback Comment:

Rollback the last batch of migrations so table is removed from database

php artisan migrate:rollback