Refresh Your Laravel Database Without Dropping Every Table

Last updated on by

Refresh Your Laravel Database Without Dropping Every Table image

Laravel's built-in migrate:fresh drops every table before re-running your migrations, which is exactly what you want until it isn't. If a table holds seed data you don't want to regenerate, or test accounts you'd rather not recreate after every change, the all-or-nothing behaviour gets in the way. Custom Fresh, by Mahmoud Ramadan, adds a fresh:custom command that rebuilds the database while preserving the tables you name.

Choosing Which Tables Survive

Say you're building a subscription billing app and iterating on the schema all day, but you don't want to lose your test accounts or the seeded plans data on every refresh. You can name the tables to keep as a comma-separated argument or use the --keep flag. Both forms do the same thing:

php artisan fresh:custom users,plans
 
php artisan fresh:custom --keep=users,plans

Everything else is dropped, and the migrations run again, so the tables you list keep their rows while the rest of the schema is rebuilt from scratch.

Glob Patterns for Grouped Tables

Cashier alone creates subscriptions and subscription_items, and listing each one by name gets tedious. Custom Fresh accepts glob patterns, so a trailing wildcard matches any table sharing a prefix:

php artisan fresh:custom "users,plans,subscription_*"

Previewing Before You Commit

Because this command is destructive, you can ask it to report what it would do without touching the database. The --explain flag prints the tables it would keep and drop:

php artisan fresh:custom users,plans,subscription_* --explain

You can also point the command at a specific connection with --database, which is handy when some data lives in a separate database:

php artisan fresh:custom monthly_revenue --database=analytics
Screenshot showing fresh:custom command
Screenshot showing the Artisan command fresh:custom dry run with --explain and also running it without a dry run

Configuration and Events

Publishing the config file lets you set defaults instead of passing the same flags on every run. Tables in always_keep survive automatically, patterns applies your glob rules without spelling them out each time, and confirm_in lists the environments where the command pauses for a confirmation prompt before it drops anything:

return [
'always_keep' => ['users', 'plans'],
'patterns' => ['subscription_*'],
'confirm_in' => ['staging', 'production'],
];

The command also dispatches three events during a run, which you can listen for to log activity or trigger follow-up work: RefreshingDatabase before any tables are dropped, TablesDropped after the removal step, and DatabaseRefreshed once the migrations finish.

Installation

Install the package with Composer:

composer require ramadan/custom-fresh

Custom Fresh requires PHP 8.2 or higher and supports Laravel 10 through 13. You can read the documentation and view the source on GitHub.

Yannick Lyn Fatt photo

Staff Writer at Laravel News and Full stack web developer.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Acquaint Softtech

Hire Laravel developers with AI expertise at $20/hr. Get started in 48 hours.

Visit Acquaint Softtech
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $9500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud

The latest

View all →
Clonio CLI: Clone Production Databases With Anonymized Data image

Clonio CLI: Clone Production Databases With Anonymized Data

Read article
Ship AI with Laravel: Test Your AI System with Zero API Calls image

Ship AI with Laravel: Test Your AI System with Zero API Calls

Read article
Laravel WhatsApp: Two Backends Behind One Facade image

Laravel WhatsApp: Two Backends Behind One Facade

Read article
Turn PHP Attributes Into Docs With Signal image

Turn PHP Attributes Into Docs With Signal

Read article
USAIGE: Track Token Usage and Costs for Laravel AI SDK Requests image

USAIGE: Track Token Usage and Costs for Laravel AI SDK Requests

Read article
Help make Filament faster! image

Help make Filament faster!

Read article