juniora/laravel-drafts
Composer 安装命令:
composer require juniora/laravel-drafts
包简介
A simple, drop-in drafts/revisions system for Laravel models
README 文档
README
A simple, drop-in drafts/revisions system for Laravel models
Version compatibility
| Laravel | Drafts |
|---|---|
| v9.x | v1.x |
| v10.x | v1.x |
| v11.x | v2.x |
| v12.x | >2.1 |
Installation
You can install the package via composer:
composer require oddvalue/laravel-drafts
You can publish the config file with:
php artisan vendor:publish --tag="drafts-config"
This is the contents of the published config file:
return [ 'revisions' => [ 'keep' => 10, ], 'column_names' => [ /* * Boolean column that marks a row as the current version of the data for editing. */ 'is_current' => 'is_current', /* * Boolean column that marks a row as live and displayable to the public. */ 'is_published' => 'is_published', /* * Timestamp column that stores the date and time when the row was published. */ 'published_at' => 'published_at', /* * UUID column that stores the unique identifier of the model drafts. */ 'uuid' => 'uuid', /* * Name of the morph relationship to the publishing user. */ 'publisher_morph_name' => 'publisher', ], 'auth' => [ /* * The guard to fetch the logged-in user from for the publisher relation. */ 'guard' => 'web', ], ];
Usage
Preparing your models
Add the trait
Add the HasDrafts trait to your model
<?php use Illuminate\Database\Eloquent\Model; use Oddvalue\LaravelDrafts\Concerns\HasDrafts; class Post extends Model { use HasDrafts; ... }
Relations
The package can handle basic relations to other models. When a draft is published HasOne and HasMany relations will be duplicated to the published model and BelongsToMany and MorphToMany relations will be synced to the published model. In order for this to happen you first need to set the $draftableRelations property on the model.
protected array $draftableRelations = [ 'posts', 'tags', ];
Alternatively you may override the getDraftableRelations method.
public function getDraftableRelations() { return ['posts', 'tags']; }
Database
The following database columns are required for the model to store drafts and revisions:
- is_current
- is_published
- published_at
- uuid
- publisher_type
- publisher_id
The names of these columns can be changed in the config file or per model using constants
e.g. To alter the name of the is_current column then you would add a class constant called IS_CURRENT
<?php use Illuminate\Database\Eloquent\Model; use Oddvalue\LaravelDrafts\Concerns\HasDrafts; class Post extends Model { use HasDrafts; public const IS_CURRENT = 'admin_editing'; ... }
There are two helper methods added to the schema builder for use in your migrations that will add/remove all these columns for you:
use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; Schema::table('posts', function (Blueprint $table) { $table->drafts(); }); Schema::table('posts', function (Blueprint $table) { $table->dropDrafts(); });
The API
The HasDrafts trait will add a default scope that will only return published/live records.
The following query builder methods are available to alter this behavior:
withoutDrafts()/published(bool $withoutDrafts = true)Only select published records (default)withDrafts(bool $withDrafts = false)Include draft recordonlyDrafts()Select only drafts, exclude published
Creating a new record
By default, new records will be created as published. You can change this either by including 'is_published' => false in the attributes of the model or by using the createDraft or saveAsDraft methods.
Post::create([ 'title' => 'Foo', 'is_published' => false, ]); # OR Post::createDraft(['title' => 'Foo']); # OR Post::make(['title' => 'Foo'])->saveAsDraft();
When saving/updating a record the published state will be maintained. If you want to save a draft of a published record then you can use the saveAsDraft and updateAsDraft methods.
# Create published post $post = Post::create(['title' => 'Foo']); # Create drafted copy $post->updateAsDraft(['title' => 'Bar']); # OR $post->title = 'Bar'; $post->saveAsDraft();
This will create a draft record and the original record will be left unchanged.
| # | title | uuid | published_at | is_published | is_current | created_at | updated_at |
|---|---|---|---|---|---|---|---|
| 1 | Foo | 9188eb5b-cc42-47e9-aec3-d396666b4e80 | 2000-01-01 00:00:00 | 1 | 0 | 2000-01-01 00:00:00 | 2000-01-01 00:00:00 |
| 2 | Bar | 9188eb5b-cc42-47e9-aec3-d396666b4e80 | 2000-01-02 00:00:00 | 0 | 1 | 2000-01-02 00:00:00 | 2000-01-02 00:00:00 |
Interacting with records
Published revision
The published revision if the live version of the record and will be the one that is displayed to the public. The default behavior is to only show the published revision.
# Get all published posts $posts = Post::all();
Current Revision
Every record will have a current revision. That is the most recent revision and what you would want to display in your admin.
To fetch the current revision you can call the current scope.
$posts = Post::current()->get();
Revisions
Every time a record is updated a new row/revision will be inserted. The default number of revisions kept is 10, this can be updated in the published config file.
You can fetch the revisions of a record by calling the revisions method.
$post = Post::find(1); $revisions = $post->revisions();
Deleting a record will also delete all of its revisions. Soft deleting records will soft delete the revisions and restoring records will restore the revisions.
If you need to update a record without creating revision
$post->withoutRevision()->update($options);
Preview Mode
Enabling preview mode will disable the global scope that fetches only published records and will instead fetch the current revision regardless of published state.
# Enable preview mode \Oddvalue\LaravelDrafts\Facades\LaravelDrafts::previewMode(); \Oddvalue\LaravelDrafts\Facades\LaravelDrafts::previewMode(true); # Disable preview mode \Oddvalue\LaravelDrafts\Facades\LaravelDrafts::disablePreviewMode(); \Oddvalue\LaravelDrafts\Facades\LaravelDrafts::previewMode(false);
Middleware
WithDraftsMiddleware
If you require a specific route to be able to access drafts then you can use the WithDraftsMiddleware middleware.
Route::get('/posts/publish/{post}', [PostController::class, 'publish'])->middleware(\Oddvalue\LaravelDrafts\Http\Middleware\WithDraftsMiddleware::class);
There is also a helper method on the router that allows you to create a group with that middleware applied.
Route::withDrafts(function (): void { Route::get('/posts/publish/{post}', [PostController::class, 'publish']); });
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.
juniora/laravel-drafts 适用场景与选型建议
juniora/laravel-drafts 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 05 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「drafts」 「Revisions」 「oddvalue」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 juniora/laravel-drafts 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 juniora/laravel-drafts 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 juniora/laravel-drafts 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Laravel package for managing model drafts.
Laravel Drafts tool for models
Effortlessly manage revisions of your Eloquent models in Filament with polymorphic user support (Fork of mansoor/filament-versionable).
Neos CMS plugin for managing content revisions
A Laravel Nova field that allows you to make drafts of your current resources.
An update for from neurony. Create multiple revisions of any Eloquent model record along with its underlying relationships
统计信息
- 总下载量: 2
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 37
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-05-09