A Simple Draft and Revision System for Laravel


July 20th, 2022

A Simple Draft and Revision System for Laravel

Laravel Drafts is a simple drop-in draft and revision system for Eloquent models. This package also supports drafts for related models; you can control saving models in a published or draft mode.

To get started with this package, your project will use the provided HasDrafts trait:

use Illuminate\Database\Eloquent\Model;
use Oddvalue\LaravelDrafts\Concerns\HasDrafts;
class Post extends Model
use HasDrafts;
// ...

With the trait configured, you can use the following API to control creating a published model or a draft:

// By default, the record is published
'title' => 'Foo',
// ...
// You can set the record as a draft in a few different ways:
// Via the `is_published` flag
'title' => 'Foo',
'is_published' => false,
// Calling `createDraft`
Post::createDraft(['title' => 'Foo']);
// Using the `saveAsDraft` method on a model
Post::make(['title' => 'Foo'])->saveAsDraft();

Similarly, you can update a model, keeping it as a draft:

$post->updateAsDraft(['title' => 'Bar']);
// Or
$post->title = 'Bar';

You can also access draft revisions and configure how many total revisions to keep in the database. The readme is an excellent place to get started with this package.

Check out this package and contribute on GitHub at oddvalue/laravel-drafts.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.