Installation
composer require laranex/laravel-schema-viewer --devThe service provider registers itself. Build the schema snapshot, then open /schema-viewer in your local environment:
php artisan schema-viewer:buildInstall it as a dev dependency unless your team wants the viewer on a server; see Access.
The schema snapshot
schema-viewer:build reads the database and your Eloquent models and writes schema-viewer.json in the project root: the schema document plus each connection's Mermaid, DBML and Markdown downloads, and changes, what changed since the previous build that changed the schema, as readable JSON. It is always written there, never under public/. The viewer has no API: its page carries the schema and the downloads from that file, the way a standalone export does, and is sent compressed. No model code runs in a web request, in any environment.
Run the build again after you migrate or change a model; the viewer shows the snapshot it last wrote. On a server, run it in your deploy script after migrate:
php artisan migrate --force
php artisan schema-viewer:buildWithout a snapshot, the viewer shows what to run instead of a diagram.
Each build compares the schema with the snapshot it replaces. When they differ, changes lists what changed: tables, columns, indexes, foreign keys, models, relations, and relationships that gained or lost a foreign key. Hints are not part of it; they have their own list. When none of that differs, the previous list is kept, so building twice changes nothing. The first build, or a build over a file from another version, writes null. The viewer's Changes tab and schema-viewer:diff show the list. Only the last step is kept: run the diff command against an older file to compare across several builds.
Building runs your models: each one is constructed, and every relation method is called. While it runs, every database connection pretends (queries are logged, never sent), model events are off, and jobs, mail, notifications, HTTP calls, events and the cache are faked; see What Runs on Build.
Commit it or ignore it
The file lists every table and column, the models' details (fillable, hidden, casts, scopes) and the source file paths, so commit it only when the repository is private.
- Commit
schema-viewer.json(private repositories only). Every clone and every server has the viewer without a database, and pull requests show schema changes as a diff of one readable file. Rebuild it with your migrations, as you would update a schema dump. - Ignore it. Add
/schema-viewer.jsonto.gitignoreand let each machine build its own: locally after migrating, on a server in the deploy script.
When you commit it, let CI catch a migration or a model change that was not rebuilt:
php artisan migrate --force && php artisan schema-viewer:build --checkThe check builds the snapshot in memory and fails when the committed file is missing or out of date; see schema-viewer:build. CI must run the same database engine with the same connection names as the machine that built the file, since each engine reports types and indexes its own way. Adding schema-viewer.json text eol=lf to .gitattributes keeps line endings stable across systems; the check accepts CRLF checkouts either way.
Access
The viewer is on in the local environment and off everywhere else. To open it on a staging or production server, turn it on and say who may see it with the viewSchema gate, for example in AppServiceProvider::boot():
use Illuminate\Support\Facades\Gate;
Gate::define('viewSchema', fn ($user) => $user->isAdmin());SCHEMA_VIEWER_ENABLED=trueWithout the gate, the viewer answers 403 outside local. Schema details help an attacker, so keep the gate strict.
To decide yourself, in every environment, call SchemaViewer::auth() in a service provider's boot(). Its callback replaces both checks (local and the gate), like Telescope::auth():
use Illuminate\Http\Request;
use Laranex\LaravelSchemaViewer\SchemaViewer;
SchemaViewer::auth(fn (Request $request): bool => $request->user()?->isAdmin() ?? false);The callback receives each request; keep request and user state out of the closure itself.
Every setting has a default; see Configuration to change one.