Description
A simple, drop-in drafts/revisions system for Laravel models
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'];}
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 |