Launch offer
Business website $150 USD Custom plugin $200 USD Ready in 5 days
Get a quote
Laravel

Creating Tables Using Migrations in Laravel

Define columns, indexes, foreign keys, and defaults when creating tables with Laravel migrations.

Creating Tables Using Migrations in Laravel — a practical guide to Laravel create table migration with clear examples you can reuse in real projects.

Laravel Tutorial Series (8/27). Prefer one page? Read the complete Laravel tutorial (all 27 topics).

Short description

Use Blueprint helpers to define columns, nullability, unique indexes, and foreign keys when creating tables.

Example: posts table with author

php artisan make:migration create_posts_table
Schema::create('posts', function (Blueprint $table) {
    $table->id();
    $table->foreignId('user_id')->constrained()->cascadeOnDelete();
    $table->string('title');
    $table->string('slug')->unique();
    $table->text('body');
    $table->boolean('is_published')->default(false);
    $table->timestamp('published_at')->nullable();
    $table->timestamps();
    $table->softDeletes();
});

Common column types

$table->string('email');
$table->integer('views')->default(0);
$table->decimal('price', 8, 2);
$table->json('meta')->nullable();
$table->enum('status', ['draft', 'published']);

Alter an existing table

php artisan make:migration add_excerpt_to_posts_table --table=posts
Schema::table('posts', function (Blueprint $table) {
    $table->string('excerpt')->nullable()->after('title');
});

Leave a reply

Your email address will not be published. Required fields are marked *