Support Online
Skip to main content

How to Create and Manage Database Tables with Laravel Migrations?

💡 What You Will Learn in This Guide

In this guide, you will learn how to create, manage and, when necessary, set up your database tables from scratch using Laravel's Migration system. The migration system allows you to manage database changes with version control in the code base.


🧠 Technical Summary

  • Migration: A method of creating, editing and deleting database tables with code.
  • Purpose: To manage the table structure without entering SQL queries.
  • Tool: Artisan command line tool (php artisan migrate).

🔧 Step 1: Preparing the Environment

Make sure you are in the root of your application and your Docker environment is running:

cd ~/landing-laravel
docker-compose up -d

This command starts Laravel, Nginx and MySQL services in the background.


🏗️ Step 2: Creating a New Migration

Run the Artisan command to create a new table:

php artisan make:migration create_linkler_tablosu

This command creates a migration file named create_linkler_tablosu in the database/migrations folder.

Your file name will be date based (for example 2025_11_05_101200_create_linkler_tablosu.php).

To find the file:

find database/migrations -name '*create_linkler_tablosu.php'

✏️ Step 3: Defining Table Columns

Open the created migration file:

nano database/migrations/2025_11_05_101200_create_linkler_tablosu.php

Edit its content as follows:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
public function up(): void
{
Schema::create('linkler', function (Blueprint $table) {
$table->id(); // Otomatik artan ID (Primary Key)
$table->string('url', 255)->unique(); // Bağlantı adresi (benzersiz)
$table->text('aciklama'); // Bağlantı açıklaması
$table->boolean('aktif')->default(true); // Durum (true/false)
$table->timestamps(); // created_at & updated_at sütunları
});
}

public function down(): void
{
Schema::dropIfExists('linkler'); // Geri alma durumunda tabloyu sil
}
};

$table->timestamps() automatically records creation and update times.


🚀 Step 4: Running Migration

Save your migration file and run it with the following command:

php artisan migrate

This command sequentially applies all migration files that have not yet been run.

The output should look like this:

Migrating: 2025_11_05_create_linkler_tablosu
Migrated: 2025_11_05_create_linkler_tablosu (120.00ms)

🔁 Step 5: Rollback and Reset

To undo migration:

php artisan migrate:rollback

This command undoes the last migration and runs the down() method.

If you want to reset all tables:

php artisan migrate:fresh

This command deletes all tables and creates them from scratch. Do not use in a production environment!


🧹 Step 6: Clean Up Unnecessary Default Migration Files

If tables such as users, password_resets, failed_jobs that come with the Laravel installation are unnecessary for this project, you can delete them.

However, it is useful to keep these files if you plan to add user management in the future.


❓ Frequently Asked Questions (FAQ)

1. Why should I use Migration instead of SQL?

Migration incorporates the database schema into version control and ensures consistency across the team.

2. What should I do to add a new column?

Create a new migration:

php artisan make:migration add_kategori_to_linkler_tablosu

3. What is the difference between Migration and Seeder?

Migration creates the database structure; Seeder adds initial data.

4. Does the migrate:fresh command delete data?

Yes, it deletes and recreates all tables. Use in development environment.

5. How do I keep migration files organized?

Create a separate migration file for each table or change; Thanks to date sorting, Laravel runs in the correct order.


🏁 Result

Laravel Migration system makes your database structure secure, versionable and rebuildable. With Artisan commands, you can create, update and retrieve tables without writing SQL.

Create a Laravel environment on the GenixNode platform and try migration processes now! 🚀