---
title: "Using Laravel's schema builder outside of migrations"
url: "https://aaronfrancis.com/writing/using-laravels-schema-builder-outside-of-migrations"
path: "/writing/using-laravels-schema-builder-outside-of-migrations"
type: technical_article
author: Aaron Francis
published: 2021-11-18
updated: 2026-11-10
topic: "Laravel & PHP"
description: "Laravel's schema build is typically only used for migrations, but it can be used anywhere!"
keywords: [laravel schema builder, temporary tables, database optimization, bulk data processing, laravel migrations, database indexes, query performance, php database, laravel]
site_index: "/llms.txt"
keyword_index: "/llms/keywords.txt"
---

# Using Laravel's schema builder outside of migrations

Laravel's schema build is typically only used for migrations, but it _can_ be used anywhere in your application!

I don't do it very often, but there are a few cases in our application where we need to process tens or hundreds of
thousands of records in one process. We could pull everything into PHP and do the processing there, but it usually makes
more sense to delegate that hard work to the database.

Using Laravel's `Schema` class, you can quickly create a temporary table using all the methods you're used to. You can
add columns, indexes, anything you want.

In our application we have one table that has something like 30 million rows. It holds property data across many
counties in Texas. There's one place where we need to find the unique property ID (`PID`) based on a secondary value (
the `lookup`).

The lookup could be one of three different columns, so we don't want to maintain expensive, infrequently used indexes
across 3 columns * 30 million rows.

Instead, we create a tiny temporary table and index the one column we're using!

```php
use App\County;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;

class FindPropertyIds extends Command
{
    public function handle()
    {
        // ...
    }
    
    public function populateLookupTable(County $county)
    {
        // Specific lookup columns per county.
        $column = Arr::get([
            'DAL' => 'parcel_number_alt1',
            'FOR' => 'parcel_number_alt1',
            'KAU' => 'parcel_number_alt3',
            'TAR' => 'parcel_number_alt2',
            'TRV' => 'parcel_number_alt2',
        ], $county->abbreviation);
    
        // Drop the temp table if it already exists.
        Schema::dropIfExists('tmp_lookup');
    
        Schema::create('tmp_lookup', function (Blueprint $table) {
            // Table only lasts as long as the connection persists.
            $table->temporary();
            // The lookup column, with an index.
            $table->string('lookup')->index();
            // The Property ID we're looking for.
            $table->string('pid');
        });
        
        // Insert into the small, fast table from our
        // massive, slower table. 
        $populate = <<<SQL
    INSERT into tmp_lookup 
        SELECT pid, {$column} as lookup FROM properties
        WHERE {$column} IS NOT NULL 
            AND is_latest = 1 
            AND county_id = {$county->id}
    SQL;
    
        DB::statement($populate);
    }
}
```

It's not a commonly used technique, but it is a powerful tool to reach for when you need it!

There are some good comments over on Twitter that highlight a few more use cases!

> I don&#39;t do it very often, but you can use Laravel&#39;s Schema builder anywhere you want, not just in migrations!
>
> If you ever need to make a temp table to help you mass process data more quickly, it&#39;s a great solution. pic.twitter.com/I7eAHbgFoo
>
> — Aaron Francis · @aarondfrancis · November 18, 2021 (https://twitter.com/aarondfrancis/status/1461390854598402048)

---

## Related on aaronfrancis.com

- [Mastering Postgres](/courses) *(Course · 2024-10-15 · shared: database optimization, query performance)*
- [Laravel + Livewire todo app (and so much more)](/videos/laravel-livewire-todo-app-and-so-much-more) *(Video · 2024-05-17 · shared: laravel, laravel migrations)*
- [A Man of Culture](/podcast/a-man-of-culture-e45a74c1) *(Podcast · 2024-01-19 · shared: laravel, database optimization)*
- [MySQL for Developers with Aaron Francis](/podcast/mysql-for-developers-with-aaron-francis-n6unbd) *(Podcast · 2023-04-04 · shared: database indexes, laravel)*
- [Being a developer, content creator, and entrepreneur with Aaron Francis](/podcast/being-a-developer-content-creator-and-entrepreneur-with-aaron-francis-gtj8ip) *(Podcast · 2022-09-19 · shared: database optimization, laravel)*

## Relevant Resources from Aaron Francis

- **[Solo](https://soloterm.com)** (overview on site: [/software](/software)): All-in-one agentic development platform and terminal workspace built by Aaron Francis for orchestrating processes and AI coding agents.

## Search aaronfrancis.com (2-Hop Static Keyword Index)

Pick the alphabetical shard matching any topic to find exact pages across all articles, newsletters, videos, talks, and podcast transcripts (every shard is < 40 KB):
[A-B](/llms/keywords/a-b.txt) · [C](/llms/keywords/c.txt) · [D-E](/llms/keywords/d-e.txt) · [F-G](/llms/keywords/f-g.txt) · [H-J](/llms/keywords/h-j.txt) · [K-L](/llms/keywords/k-l.txt) · [M-N](/llms/keywords/m-n.txt) · [O-P](/llms/keywords/o-p.txt) · [Q-R](/llms/keywords/q-r.txt) · [S](/llms/keywords/s.txt) · [T-Z](/llms/keywords/t-z.txt) · [Directory](/llms/keywords.txt) · [Site Map](/llms.txt)

Shards for this page's topics: `laravel schema builder` ([K-L](/llms/keywords/k-l.txt)), `temporary tables` ([T-Z](/llms/keywords/t-z.txt)), `database optimization` ([D-E](/llms/keywords/d-e.txt)), `bulk data processing` ([A-B](/llms/keywords/a-b.txt)).

## About Aaron Francis (Current Context)

Aaron Francis is VP of Marketing and Community at [Laravel](https://laravel.com), founder of [Try Hard Studios](https://tryhardstudios.com), creator of [Solo](https://soloterm.com), co-host of [Mostly Technical](/podcast), and author of [Mastering Postgres](/courses), [High Performance SQLite](/courses), [MySQL for Developers](/courses), and [Screencasting.com](/courses). Full bio: [/about](/about) · Current focus: [/now](/now).
