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:
使用
照亮\数据库\雄辩\模型
;使用
JacobJoergensen\LaravelPaper\Attributes\ContentPath
;使用
JacobJoergensen\LaravelPaper\Attributes\Driver
;使用
JacobJoergensen\LaravelPaper\Paper
;#[
司机
(
'markdown'
)]#[
ContentPath
(
'content/docs'
)]班级
文档
延伸
模型{
使用
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
=
文档
::
在哪里
(
'版本'
,
'v1.0'
)
->
orderBy
(
'优先事项'
,
'升序'
)
->
得到
();// Retrieve a specific page by its filename$setup
=
文档
::
寻找
(
'initial-setup'
(英文):// Search within frontmatter arrays or text$tagged
=
文档
::
whereContains
(
'labels'
,
‘api’
)
->
得到
();$found
=
文档
::
哪里喜欢
(
'subtitle'
,
'%configuration%'
)
->
得到
();
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:
民众
功能
类别
(){
// Resolves based on the 'category_slug' key in the Markdown frontmatter
返回
$this
->
belongsToPaper
(
类别
::班级
(英文):}民众
功能
subPages
(){
返回
$this
->
hasManyPaper
(
文档
::班级
(英文):}
Managing Files through Eloquent
Laravel Paper supports full write capabilities. When you call
save()
或者
delete()
, the package performs the corresponding filesystem operation, keeping your content in sync with your model state:
// Creating a new file$doc
=
新的
文档
();$doc
->
蛞蝓
=
'new-guide'
;$doc
->
标题
=
'Architecture Overview'
;$doc
->
内容
=
'Markdown content starts here...'
;$doc
->
节省
();
// Creates content/docs/new-guide.md// Deleting an existing file$outdated
=
文档
::
寻找
(
'old-api-guide'
(英文):$outdated
?->
删除
();
// Removes the file from disk
安装
You can add the package to your project via Composer:
作曲家
要求
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:
---标题
:
架构概述版本
:
v1.0标签
:[
api
,
核
]优先事项
:
1---Markdown content starts here...
The source code and further documentation for custom drivers are available on GitHub 。







