Blog Detail

26

Jul
A simple drop-in drafts/revisions system for Laravel models cover image

arrow_back A simple drop-in drafts/revisions system for Laravel models

Oddvalue introduced an amazing package Laravel Drafts which is a very 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.

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="laravel-drafts-config"

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 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 record
  • onlyDrafts() Select only drafts, exclude published

Interacting with records

Published revision

The published revision is 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();

You can implement a preview mode for your front end by calling the current scope when fetching records.

For more details, You can visit Github.

Published at : 26-07-2022

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project