optimistdigital / nova-drafts
A Laravel Nova field that allows you to make drafts of your current resources.
Installs: 41 155
Dependents: 0
Suggesters: 0
Security: 0
Stars: 28
Watchers: 3
Forks: 7
Open Issues: 7
Requires
- php: >=7.1.0
This package is auto-updated.
Last update: 2023-06-07 21:31:13 UTC
README
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
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.
Defining the field
use OptimistDigital\NovaDrafts\DraftButton; use OptimistDigital\NovaDrafts\PublishedField; use OptimistDigital\NovaDrafts\UnpublishButton; public function fields(Request $request) { return [ 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