1. Go to this page and download the library: Download oddvalue/laravel-drafts library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
oddvalue / laravel-drafts example snippets
return [
'revisions' => [
'keep' => 10,
],
'column_names' => [
/*
* Boolean column that marks a row as the current version of the data for editing.
*/
'is_current' => 'is_current',
/*
* Boolean column that marks a row as live and displayable to the public.
*/
'is_published' => 'is_published',
/*
* Boolean column that marks a row as an auto draft: an auto-saved
* working copy that is updated in place and never published.
*/
'is_auto' => 'is_auto',
/*
* Timestamp column that stores the date and time when the row was published.
*/
'published_at' => 'published_at',
/*
* UUID column that stores the unique identifier of the model drafts.
*/
'uuid' => 'uuid',
/*
* Name of the morph relationship to the publishing user.
*/
'publisher_morph_name' => 'publisher',
],
'auth' => [
/*
* The guard to fetch the logged-in user from for the publisher relation.
*/
'guard' => 'web',
],
];
use Illuminate\Database\Eloquent\Model;
use Oddvalue\LaravelDrafts\Concerns\HasDrafts;
use Oddvalue\LaravelDrafts\Contracts\Draftable;
class Post extends Model implements Draftable
{
use HasDrafts;
...
}
public function getDraftableRelations()
{
return ['posts', 'tags'];
}
use Illuminate\Database\Eloquent\Model;
use Oddvalue\LaravelDrafts\Concerns\HasDrafts;
class Post extends Model
{
use HasDrafts;
public const IS_CURRENT = 'admin_editing';
...
}
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
Schema::table('posts', function (Blueprint $table) {
$table->drafts();
});
Schema::table('posts', function (Blueprint $table) {
$table->dropDrafts();
});
Post::create([
'title' => 'Foo',
'is_published' => false,
]);
# OR
Post::createDraft(['title' => 'Foo']);
# OR
Post::make(['title' => 'Foo'])->saveAsDraft();
# Create published post
$post = Post::create(['title' => 'Foo']);
# Create drafted copy
$post->updateAsDraft(['title' => 'Bar']);
# OR
$post->title = 'Bar';
$post->saveAsDraft();
$post = Post::find(1);
# Create or update the record's auto draft
$post->saveAsAutoDraft(['title' => 'Partially typed title']);
# Retrieve it
$autoDraft = $post->autoDraft;
# Check whether a revision is an auto draft
$autoDraft->isAutoDraft();
# Discard it
$post->discardAutoDraft();
'scheduled_drafts' => [
'enabled' => true,
],
$post = Post::find(1);
$draft = $post->createDraft(['title' => 'Hello World']);
# Schedule the draft to be published in a week (saves the record)
$draft->schedulePublishing(now()->addWeek());
# Change your mind and clear the schedule (remember to save)
$draft->clearScheduledPublishing()->save();
use Oddvalue\LaravelDrafts\Commands\PublishScheduledDrafts;
Schedule::command(PublishScheduledDrafts::class, [Post::class])->everyMinute();