Laravel Packages

Laravel Paper: A Flat-File Eloquent Driver

Published Updated
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.

Sponsored

masteringlaravel logo
Laravel Code Review

Get expert guidance in a few days with a Laravel code review

Visit Laravel Code Review

The latest

View all →
CPX: The Composer Package Executor for PHP image

CPX: The Composer Package Executor for PHP

Read article
Laravel AI SDK Adds Human-in-the-Loop Tool Approval image

Laravel AI SDK Adds Human-in-the-Loop Tool Approval

Read article
Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals image

Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals

Read article
Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs image

Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs

Read article
Blade Formatting in Laravel Pint image

Blade Formatting in Laravel Pint

Read article
Inertia DevTools Is Now on the Chrome Web Store image

Inertia DevTools Is Now on the Chrome Web Store

Read article