|
| 1 | +## Laravel Drafts |
| 2 | + |
| 3 | +A simple, drop-in drafts/revisions system for Laravel Eloquent models. |
| 4 | + |
| 5 | +### Features |
| 6 | + |
| 7 | +- Drafts and revisions for any Eloquent model via a single trait. |
| 8 | +- Schema helper macros to add or remove required columns. |
| 9 | +- Query scopes for published, drafts, and current revisions. |
| 10 | +- Preview mode and middleware for draft access. |
| 11 | + |
| 12 | +### Installation |
| 13 | + |
| 14 | +@verbatim |
| 15 | +<code-snippet name="Install package" lang="bash"> |
| 16 | +composer require oddvalue/laravel-drafts |
| 17 | +php artisan vendor:publish --tag="drafts-config" |
| 18 | +</code-snippet> |
| 19 | +@endverbatim |
| 20 | + |
| 21 | +### Required model setup |
| 22 | + |
| 23 | +- Add the `HasDrafts` trait to the model. |
| 24 | +- Ensure the model table has the draft columns (use the schema macros). |
| 25 | +- Optionally define `$draftableRelations` if you want relations copied/synced on publish. |
| 26 | + |
| 27 | +@verbatim |
| 28 | +<code-snippet name="Model setup" lang="php"> |
| 29 | +use Illuminate\Database\Eloquent\Model; |
| 30 | +use Oddvalue\LaravelDrafts\Concerns\HasDrafts; |
| 31 | + |
| 32 | +class Post extends Model |
| 33 | +{ |
| 34 | + use HasDrafts; |
| 35 | + |
| 36 | + protected array $draftableRelations = [ |
| 37 | + 'tags', |
| 38 | + ]; |
| 39 | +} |
| 40 | +</code-snippet> |
| 41 | +@endverbatim |
| 42 | + |
| 43 | +@verbatim |
| 44 | +<code-snippet name="Migration helpers" lang="php"> |
| 45 | +use Illuminate\Database\Schema\Blueprint; |
| 46 | +use Illuminate\Support\Facades\Schema; |
| 47 | + |
| 48 | +Schema::table('posts', function (Blueprint $table) { |
| 49 | + $table->drafts(); |
| 50 | +}); |
| 51 | +</code-snippet> |
| 52 | +@endverbatim |
| 53 | + |
| 54 | +### Creating drafts and publishing |
| 55 | + |
| 56 | +- New records are published by default. |
| 57 | +- Use `createDraft`, `saveAsDraft`, or `updateAsDraft` to keep the published record unchanged. |
| 58 | + |
| 59 | +@verbatim |
| 60 | +<code-snippet name="Create or update a draft" lang="php"> |
| 61 | +Post::createDraft(['title' => 'Draft title']); |
| 62 | + |
| 63 | +$post = Post::create(['title' => 'Live title']); |
| 64 | +$post->updateAsDraft(['title' => 'Draft edit']); |
| 65 | +</code-snippet> |
| 66 | +@endverbatim |
| 67 | + |
| 68 | +### Querying revisions |
| 69 | + |
| 70 | +@verbatim |
| 71 | +<code-snippet name="Scopes" lang="php"> |
| 72 | +$published = Post::withoutDrafts()->get(); |
| 73 | +$withDrafts = Post::withDrafts()->get(); |
| 74 | +$draftsOnly = Post::onlyDrafts()->get(); |
| 75 | +$current = Post::current()->get(); |
| 76 | +</code-snippet> |
| 77 | +@endverbatim |
| 78 | + |
| 79 | +### Preview mode and middleware |
| 80 | + |
| 81 | +@verbatim |
| 82 | +<code-snippet name="Preview mode" lang="php"> |
| 83 | +use Oddvalue\LaravelDrafts\Facades\LaravelDrafts; |
| 84 | + |
| 85 | +LaravelDrafts::previewMode(); |
| 86 | +LaravelDrafts::disablePreviewMode(); |
| 87 | +</code-snippet> |
| 88 | +@endverbatim |
| 89 | + |
| 90 | +@verbatim |
| 91 | +<code-snippet name="WithDraftsMiddleware" lang="php"> |
| 92 | +use Oddvalue\LaravelDrafts\Http\Middleware\WithDraftsMiddleware; |
| 93 | + |
| 94 | +Route::withDrafts(function (): void { |
| 95 | + Route::get('/posts/publish/{post}', [PostController::class, 'publish']); |
| 96 | +}); |
| 97 | +</code-snippet> |
| 98 | +@endverbatim |
0 commit comments