Laravel Cloud is here! Zero-config managed infrastructure for Laravel apps. Deploy now.

Laravel Paper: A Flat-File Eloquent Driver

Last updated on by

Laravel Paper: A Flat-File Eloquent Driver image

Laravel Paper, by Jacob Jørgensen, brings Eloquent's feature set to flat-file data sources. It allows you to treat Markdown and JSON files as database records, which is useful for documentation, product catalogs, or small-scale content management where a traditional database might be unnecessary.

Turning Files into Models

By applying the Paper trait and PHP 8 attributes, you can map a model to a specific directory on your disk. You don't need to configure a separate database connection or run migrations to get started:

use Illuminate\Database\Eloquent\Model;
use JacobJoergensen\LaravelPaper\Attributes\ContentPath;
use JacobJoergensen\LaravelPaper\Attributes\Driver;
use JacobJoergensen\LaravelPaper\Paper;
 
#[Driver('markdown')]
#[ContentPath('content/docs')]
class Document extends Model
{
use Paper;
}

Querying File Data

Since the driver implements the Eloquent interface, you can use standard query builder methods to filter and sort your files. The slug is derived from the filename automatically, acting as the primary identifier:

// Fetch all documentation pages for a specific version
$docs = Document::where('version', 'v1.0')
->orderBy('priority', 'asc')
->get();
 
// Retrieve a specific page by its filename
$setup = Document::find('initial-setup');
 
// Search within frontmatter arrays or text
$tagged = Document::whereContains('labels', 'api')->get();
$found = Document::whereLike('subtitle', '%configuration%')->get();

File-Based Relationships

The package includes methods for linking different flat-file models. This allows you to define associations based on fields stored in your files' frontmatter:

public function category()
{
// Resolves based on the 'category_slug' key in the Markdown frontmatter
return $this->belongsToPaper(Category::class);
}
 
public function subPages()
{
return $this->hasManyPaper(Document::class);
}

Managing Files through Eloquent

Laravel Paper supports full write capabilities. When you call save() or delete(), the package performs the corresponding filesystem operation, keeping your content in sync with your model state:

// Creating a new file
$doc = new Document();
$doc->slug = 'new-guide';
$doc->title = 'Architecture Overview';
$doc->content = 'Markdown content starts here...';
$doc->save(); // Creates content/docs/new-guide.md
 
// Deleting an existing file
$outdated = Document::find('old-api-guide');
$outdated?->delete(); // Removes the file from disk

Installation

You can add the package to your project via Composer:

composer require jacobjoergensen/laravel-paper

Laravel Paper requires PHP 8.4 and Laravel 12.0 or higher. To use the Markdown driver, your files should include YAML frontmatter for metadata:

---
title: Architecture Overview
version: v1.0
labels: [api, core]
priority: 1
 
---
Markdown content starts here...

The source code and further documentation for custom drivers are available 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
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
Shift logo

Shift

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

Shift
PhpStorm logo

PhpStorm

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

PhpStorm
SerpApi logo

SerpApi

Access real-time search engine results through a simple API—no more scraping headaches! Use it for AI applications, SEO tools, product research, travel information, and more

SerpApi
Tinkerwell logo

Tinkerwell

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

Tinkerwell
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
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
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
Lucky Media logo

Lucky Media

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

Lucky Media
Laravel Cloud logo

Laravel Cloud

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

Laravel Cloud

The latest

View all →
Moat: A Security Review for Your GitHub Account image

Moat: A Security Review for Your GitHub Account

Read article
Simple Feature Flags for Laravel with Laravel Toggle image

Simple Feature Flags for Laravel with Laravel Toggle

Read article
Manage Laravel Cloud Deployments Inside PhpStorm image

Manage Laravel Cloud Deployments Inside PhpStorm

Read article
Piper: Laravel-Style Array and String Helpers for PHP's Pipe Operator image

Piper: Laravel-Style Array and String Helpers for PHP's Pipe Operator

Read article
Storage Cache Store in Laravel 13.10.0 image

Storage Cache Store in Laravel 13.10.0

Read article
Laravel MongoDB Full-Text Search tutorial: The Art of the Relevancy image

Laravel MongoDB Full-Text Search tutorial: The Art of the Relevancy

Read article