A Simple Draft and Revision System for Laravel

Packages

July 20th, 2022

laravel-drafts.png

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:

1use Illuminate\Database\Eloquent\Model;
2use Oddvalue\LaravelDrafts\Concerns\HasDrafts;
3 
4class Post extends Model
5{
6 use HasDrafts;
7 // ...
8}

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

1// By default, the record is published
2Post::create([
3 'title' => 'Foo',
4 // ...
5]);
6 
7// You can set the record as a draft in a few different ways:
8 
9// Via the `is_published` flag
10Post::create([
11 'title' => 'Foo',
12 'is_published' => false,
13]);
14 
15// Calling `createDraft`
16Post::createDraft(['title' => 'Foo']);
17 
18// Using the `saveAsDraft` method on a model
19Post::make(['title' => 'Foo'])->saveAsDraft();

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

1$post->updateAsDraft(['title' => 'Bar']);
2 
3// Or
4 
5$post->title = 'Bar';
6$post->saveAsDraft();

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.

Laravel News Partners