Laravel in a Container
Migrations & Eloquent
Lesson 3 of 4
What you'll learn
- Write a migration that builds a Postgres table
- Understand migrations as version control for your schema
- Read and write rows with an Eloquent model
You don't create Postgres tables by hand — you write migrations: PHP files that describe schema changes and run in order. They're version control for your database, so every environment (your laptop, the container, production) ends up identical.
// database/migrations/2026_05_31_000000_create_posts_table.php
use Illuminate\Database\Schema\Blueprint;
public function up(): void
{
Schema::create('posts', function (Blueprint $table) {
$table->id(); // BIGSERIAL PRIMARY KEY in Postgres
$table->string('title');
$table->text('body')->nullable();
$table->timestamps(); // created_at, updated_at
});
}
Run it inside the container: docker compose exec app php artisan migrate. Laravel translates that Blueprint into Postgres DDL and records which migrations have run.
Eloquent
An Eloquent model is a class that maps to a table. Once it exists, you read and write rows with expressive methods — no SQL strings:
class Post extends Model {}
$post = Post::create(['title' => 'Hello', 'body' => 'First post']);
$all = Post::where('title', 'Hello')->get();
$one = Post::find(1);
Convention over configuration
The Post model maps to the posts table automatically (plural, snake_case), uses id as the key, and manages created_at/updated_at for you. Learn the conventions and you write far less config.
The challenge is a fluent schema builder — the same idea as a migration's Blueprint, compiling to Postgres DDL. Run it.
The builder records columns and emits Postgres DDL — like Blueprint does. Run it, then add a column.
Next: connecting tables together with relationships and querying them.
Sign in to save your progress across devices.