optimistdigital/nova-drafts

A Laravel Nova field that allows you to make drafts of your current resources.

1.1.1 2020-05-25 13:15 UTC

README

Latest Version on Packagist Total Downloads

This Laravel Nova field allows you to make drafts of your resources.

Features

  • Create Drafts
  • Unpublish resources (disable them)
  • Artisan Command to create Migration

Screenshots

Detail View

Form View

Index View

Installation

Install the package in a Laravel Nova project via Composer:

composer require optimistdigital/nova-drafts

Usage

Preparing the models and database

This field requires a few database changes - namely, the model requires three new columns Migrations can be created using the following Artisan command:

php artisan drafts:migration {table?}

if table name is not provided, a choice of all available tables is provided.

If your table has pre-existing Unique constraint: It's recommended to add 'published' field to the unique constraint. More information inside the migration file.

Creating the field

use OptimistDigital\NovaDrafts\DraftButton;
use OptimistDigital\NovaDrafts\PublishedField;
use OptimistDigital\NovaDrafts\UnpublishButton;

UnpublishButton::make('Unpublish'),
DraftButton::make('Draft'),
PublishedField::make('State', 'published'),

Index Filtering (IndexQuery)

Inside your Model add the following snippet:

public function childDraft()
{
    return $this->hasOne(YourModelName::class, 'draft_parent_id', 'id');
}

Inside your Index Query use that function, to filter out published pages that have drafts:

public static function indexQuery(NovaRequest $request, $query)
{
    return $query->doesntHave('childDraft');
}

Options

Possible option you can pass to the field using the option name as a function

Option Type Default Description
draftsEnabled boolean true boolean whether drafts are enabled or not

Localization and text customization

The translation file(s) can be published by using the following publish command:

php artisan vendor:publish --provider="OptimistDigital\NovaDrafts\FieldServiceProvider" --tag="translations"

You can add your translations to resources/lang/vendor/nova-drafts/ by creating a new translations file with the locale name (ie et.json) and copying the JSON from the existing en.json.

Credits

License

Nova Drafts is open-sourced software licensed under the MIT license