[ Blog ] → Laravel

Database design decisions that save you later

The schema decisions we make early on every Laravel project, with the migrations for constraints, types, keys, tenancy, history and live changes.

Rainer Rodriguez

Co-founder, Nerdify

Sep 1, 2026 · 8 min read

Good database design comes down to a handful of early decisions: normalize the data and denormalize only with a measured reason, let the database enforce integrity with foreign keys, unique and NOT NULL constraints, choose column types you won't have to migrate away from, index for the queries you run, and plan how the schema will change while the app is live. These are cheap to get right in the first migration and expensive to fix once a table has millions of rows and three apps reading it.

This is how we handle each one on Laravel projects, on MySQL or PostgreSQL, with the migrations we write for them.

Normalize first, denormalize with a reason

We start with a normalized schema: each fact stored once, in the table it belongs to. A customer's email lives on the customer, not copied onto every order. When the email changes, one row changes.

Denormalizing is fine when a measured query needs it. An orders.total_cents column that duplicates the sum of the order lines saves a join and a SUM on every list page, and it is worth it once the list is slow. The rule we follow is that the copy has one owner in the code, usually a method that recalculates it in the same transaction that changes the lines, so it can't drift.

What we avoid is denormalizing on day one because a query might be slow someday. Most of those queries are fine with an index.

Let the database enforce the rules

Validation in the application is necessary and not enough. A queue job, a console command, a second app or a developer in a database client can all write rows that skip your form request. The constraints in the schema are the only rules every writer has to follow.

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('memberships', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('team_id')->constrained()->cascadeOnDelete();
            $table->foreignId('user_id')->constrained()->restrictOnDelete();
            $table->string('role');
            $table->timestamps();

            $table->unique(['team_id', 'user_id']);
        });
    }
};

Every relation gets a real foreign key, and we decide the delete behavior for each one instead of leaving the default. Deleting a team removes its memberships. Deleting a user who still has memberships fails, which is what we want, because that should be a deliberate action in the app with its own cleanup.

The composite unique index means a user can't join the same team twice, no matter how many times the invite link is clicked. Columns are NOT NULL unless null means something specific. A nullable column with no clear meaning for null ends up with three states (null, empty string, a value) that every query has to handle.

Choose types you won't regret

Money is stored as an integer number of cents, with a currency column next to it. Floats can't represent most decimal amounts exactly, and the rounding errors end up on invoices. DECIMAL(12, 2) is also correct, but integers are simpler to work with in PHP and JavaScript, and they serialize without surprises in an API.

Timestamps are stored in UTC and converted for display. Laravel's default app timezone is UTC, and we leave it that way, even for clients in a single timezone. Daylight saving time changes have a way of finding the one report that assumed local time.

The constraints in the schema are the only rules every writer has to follow.

For statuses and types we use a string column and a PHP backed enum, instead of a database ENUM column:

php
enum OrderStatus: string
{
    case Draft = 'draft';
    case Paid = 'paid';
    case Shipped = 'shipped';
    case Cancelled = 'cancelled';
}
php
final class Order extends Model
{
    /**
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'status' => OrderStatus::class,
            'paid_at' => 'immutable_datetime',
        ];
    }
}

Adding a status is then a code change with no migration. A MySQL ENUM needs an ALTER TABLE for every new value, and any change other than appending a value at the end of the list rebuilds the table.

JSON columns are for data that is unstructured or owned by someone else, such as a webhook payload we keep for debugging, or settings whose keys vary per customer. If we filter, sort or join on a value, it gets its own column.

Pick keys that don't leak

Internal primary keys are auto-incrementing big integers. They are compact, fast to index and easy to read in logs. When an ID appears in a public URL or an API response that outsiders see, we add a ULID, so the URL doesn't reveal how many orders exist or invite someone to try the next number:

php
$table->id();
$table->ulid('public_id')->unique();

Some teams use ULIDs or UUIDs as the primary key everywhere, with Laravel's HasUlids trait. It works, and the trade-off is larger indexes and foreign keys. ULIDs at least sort by creation time, which keeps inserts at the end of the index, unlike random UUID v4 values. Since Laravel 12, HasUuids generates UUID v7 values, which are time-ordered for the same reason.

Put the tenant on every row

Much of what we build is multi-tenant: many companies in one database, each seeing only its own data. Every tenant-owned table gets a team_id (or account_id, whatever the domain calls it), including child tables where the tenant could be found through a join. Having it on the row lets every query filter directly, and lets every composite index start with it:

php
$table->index(['team_id', 'status', 'created_at']);

Leaving the tenant off child tables saves a column and costs you on every query and every authorization check, because you have to join up to the parent to know who owns a row.

Use soft deletes sparingly

Laravel's SoftDeletes makes deletes reversible, and that is useful for records people delete by mistake and want back. It also means every query on that table carries deleted_at IS NULL, unique indexes stop working the way people expect (a soft-deleted user still holds their email), and reports have to decide each time whether deleted rows count.

We use soft deletes for a few user-facing records where undo matters. For history and compliance, we keep an audit log instead.

Keep history in its own table

When a client needs to know who changed what, a last_updated_by column isn't enough. On 1Click's package-tracking platform, the audit trail records who made each change, when, the values before and after, and why. That lives in its own table:

php
Schema::create('audit_logs', function (Blueprint $table): void {
    $table->id();
    $table->foreignId('user_id')->nullable()->constrained()->nullOnDelete();
    $table->morphs('auditable');
    $table->string('event');
    $table->json('before')->nullable();
    $table->json('after')->nullable();
    $table->string('reason')->nullable();
    $table->timestamp('created_at')->useCurrent();

    $table->index(['auditable_type', 'auditable_id', 'created_at']);
});

The audit table is append-only. Nothing in the app updates or deletes its rows, and it has no updated_at. The nullable user_id is one of the few nulls we accept, because some changes come from the system and a removed user's history must survive.

Change the schema without stopping the app

Schemas keep changing after launch, so we design migrations for a database that is serving traffic. Changes are additive where possible. To rename a column, we add the new one, deploy code that writes to both, backfill, switch reads, and drop the old column in a later release. Renaming in one step breaks the old code that is still serving requests during the deploy.

Our migrations have no down() method. We have never had a reason to trust a rollback that only ran on a laptop, so we roll forward. If a migration was wrong, the fix is a new migration. Data changes, like backfilling that new column, run separately from schema migrations as one-off operations, with a dry run against production data before the real run.

Before any migration on a large table, we check what it locks. Adding a nullable column is fast on current MySQL and PostgreSQL versions. On PostgreSQL, CREATE INDEX without CONCURRENTLY blocks writes for as long as the build takes. MySQL builds most secondary indexes online, but changing a column's type still copies the whole table.

Read also · Product development

How we make slow web apps fast

Sep 15, 2026 · 8 min read

[ Read → ]

If you want a team that designs schemas this way from the first migration, or one to review a database that has grown in ways nobody planned, see our Laravel development services.

Written by

Rainer Rodriguez

Co-founder, Nerdify

Rainer co-founded Nerdify in Managua, Nicaragua, in 2016 and works with the team on the client products it builds and maintains.

Service · Laravel development

Need a team that works like this?

More from the blog

[ All articles → ]