Laravel Migrations: Creating and Managing Database Tables
Meta Description:
Learn how to create, update, and version database tables without writing SQL using Laravel Migrations.
What will you learn in this guide?
You will learn how to manage your application's database schema via the command line using the Laravel Migrations feature. We will walk you through how to create a table with the Artisan tool and add custom columns to this table. In this way, you will be able to manage your database like a version control system.
🧠 Stage 1 – Analyze Content (Comprehension)
Main Technical Topic:
Creating and managing database tables programmatically using Migrations in the Laravel framework.
Which Problem Does It Solve?
Enabling developers to easily create, destroy and recreate database schema, eliminating the need to write SQL queries or log into the database console. This facilitates cross-team collaboration and continuous integration.
What Steps Does the User Follow?
- Going to the project root directory and starting the Docker environment.
- Creating a new migration file with the
php artisan make:migrationcommand. - Open the created migration file and define the columns (url, description, enabled) to be added to the table using the
up()method. - Checking the
down()method to delete the table (Schema::dropIfExists). - Running migrations with the
php artisan migratecommand.
Technical Summary:
This guide aims to write a Migration to create the links table in Laravel application. Steps: Create the create_links_table migration file with Artisan, add the string('url'), text('description'), boolean('enabled') columns to the up() method in this file, and finally activate the table in the database with php artisan migrate.
Stage 2 – Make a Technically Accurate and Localized Translation into Turkish
1. Preparation and Creating a New Immigration File
First, make sure your development environment is working. Then, let's create a new migration file with Laravel's Artisan command line tool.
Starting the Docker Environment
Check that you are in the root of the application and run the Docker Compose environment:
cd ~/genixnode-laravel-app
docker-compose up -d
This command boots up your application's development environment in the background.
Creating the Migration File
Let's create a migration file for the links table where we will store the links in our database:
docker-compose exec app php artisan make:migration create_links_table
This command creates a new PHP class with timestamp in the database/migrations folder. Check the exact name of the new file in the (örneğin 2025_10_21_120000_create_links_table.php) command output.
2. Defining Table Columns
The created migration file contains two main methods:
up(): Works when you run the migration (creating the table).
down(): Works when you undo the migration (deleting the table).
Open the file with your text editor. (Your sample file name will be different.)
nano database/migrations/2025_10_21_120000_create_links_table.php
Updating the up() Method
Let's add the columns we need into the Schema::create('links', ...) structure. Keep the default methods id() and timestamps(). The following code is the new contents of your create_links_table.php file:
// database/migrations/2025_10_21_120000_create_links_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateLinksTable extends Migration
{
public function up()
{
Schema::create('links', function (Blueprint $table) {
$table->id(); // Otomatik artan birincil anahtar
$table->string('url', 200); // 200 karakterlik bağlantı URL'si
$table->text('description'); // Uzun metin alanı (açıklama)
$table->boolean('enabled')->default(true); // Durum bilgisi, varsayılanı true
$table->timestamps(); // create_at ve updated_at sütunları
});
}
public function down()
{
Schema::dropIfExists('links'); // Tabloyu siler
}
}
This code adds the URL, description, and event status columns to the links table.
Save and close the file.
3. Running and Validating Migrations
Now that we've defined the columns, let's run migrations to have Artisan create this table in your database.
Running Migration
To run Migration, use this command:
docker-compose exec app php artisan migrate
This command creates all tables that have not yet been created in the database, including the links table.
In the output, you should see the line Migrating: ...create_links_table. This means your table has been created successfully.
Rollback of Migrations
If you make a mistake or want to change the structure of the table, you can undo the last migration:
docker-compose exec app php artisan migrate:rollback
This command rolls back the last run migrations and deletes the links table.
Frequently Asked Questions (FAQ)
- What is the main advantage of using Migration?
The biggest advantage is versioning the database schema. Since team members work from the same migration files, it ensures that everyone's database structure is the same. It also eliminates the need to write SQL.
- What is the difference between
Schema::createandSchema::table?
Schema::create('tablo_adi', ...): Creates a new table.
Schema::table('tablo_adi', ...): Adds a new column to an existing table or replaces existing columns.
- What happens if I manually add a table to the database?
The artisan migrate command only runs migration files that have no record in the migrations table. Manually added tables are not included in this system. This can lead to schema inconsistency. That's why it's recommended to do all table operations with Migration.
- Why is there a date at the beginning of the file name?
The timestamp at the beginning of the migration files determines the order in which Laravel runs the migrations. This is critical for managing cross-table dependencies. The file with the oldest date runs first.
- What columns does the
timestamps()method create?
This helper method adds two columns that are automatically updated and created with each database record:
created_at
updated_at
Result
You have successfully managed your database table with Laravel Migrations. You can now update and manage your database schema more easily. You can quickly continue building the next stages of your application on the GenixNode platform.

