leandrodiogenes / laravel-drafts
为 Laravel 模型提供简单、即插即用的草稿/修订系统
Requires
- php: ^7.4
- doctrine/dbal: ^3.3
- illuminate/contracts: ^8.0
- nesbot/carbon: ^2.59
- spatie/laravel-package-tools: ^1.9.2
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.8
- nunomaduro/collision: ^v5.0
- nunomaduro/larastan: ^1.0
- orchestra/testbench: ^6.0
- pestphp/pest: ^1.21
- pestphp/pest-plugin-laravel: ^1.1
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.5
- spatie/laravel-ray: ^1.26
- spatie/test-time: ^1.3
README
为 Laravel 模型提供简单、即插即用的草稿/修订系统
安装
您可以通过 composer 安装此包
composer require oddvalue/laravel-drafts
您可以使用以下命令发布配置文件:
php artisan vendor:publish --tag="laravel-drafts-config"
这是已发布配置文件的内容
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', /* * 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', ], ];
使用
准备你的模型
添加特性
将 HasDrafts
特性添加到您的模型中
<?php use Illuminate\Database\Eloquent\Model; use Oddvalue\LaravelDrafts\Concerns\HasDrafts; class Post extends Model { use HasDrafts; ... }
关系
此包可以处理与其他模型的基本关系。当草稿发布时,HasOne
和 HasMany
关系将复制到发布模型,而 BelongsToMany
和 MorphToMany
关系将同步到发布模型。为了实现这一点,您首先需要在模型上设置 $draftableRelations
属性。
protected array $draftableRelations = [ 'posts', 'tags', ];
或者,您可以重写 getDraftableRelations
方法。
public function getDraftableRelations() { return ['posts', 'tags']; }
数据库
以下数据库列对于模型存储草稿和修订是必需的
- is_current
- is_published
- published_at
- uuid
- publisher_type
- publisher_id
这些列的名称可以在配置文件中或使用常量按模型更改
例如,要更改 is_current
列的名称,则您需要添加一个名为 IS_CURRENT
的类常量
<?php 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(); });
API
HasDrafts
特性将添加一个默认范围,该范围仅返回已发布/活动记录。
以下查询构建器方法可用于更改此行为
withoutDrafts()
/published(bool $withoutDrafts = true)
仅选择已发布的记录(默认)withDrafts(bool $withDrafts = false)
包含草稿记录onlyDrafts()
选择仅草稿,排除已发布的
创建新记录
默认情况下,新记录将作为已发布创建。您可以通过将 'is_published' => false
包含在模型的属性中或使用 createDraft
或 saveAsDraft
方法来更改此设置。
Post::create([ 'title' => 'Foo', 'is_published' => false, ]); # OR Post::createDraft(['title' => 'Foo']); # OR Post::make(['title' => 'Foo'])->saveAsDraft();
当保存/更新记录时,将保持发布状态。如果要将已发布的记录的草稿保存,则可以使用 saveAsDraft
和 updateAsDraft
方法。
# Create published post $post = Post::create(['title' => 'Foo']); # Create drafted copy $post->updateAsDraft(['title' => 'Bar']); # OR $post->title = 'Bar'; $post->saveAsDraft();
这将创建一个草稿记录,原始记录将保持不变。
与记录交互
已发布修订版
已发布修订版是记录的实时版本,将是向公众显示的版本。默认行为是仅显示已发布的修订版。
# Get all published posts $posts = Post::all();
当前修订版
每个记录都有一个当前修订版。这是最新的修订版,您希望在管理界面中显示的。
要获取当前修订版,您可以调用 current
范围。
$posts = Post::current()->get();
您可以通过在获取记录时调用 current
范围来实现前端的前瞻模式。
修订版
每次更新记录时,都会插入一个新的行/修订版。默认保留的修订版数量为 10,这可以在发布的配置文件中更新。
您可以通过调用 revisions
方法获取记录的修订版。
$post = Post::find(1); $revisions = $post->revisions();
删除记录也会删除其所有修订版。软删除记录将软删除修订版,并恢复记录将恢复修订版。
测试
composer test
变更日志
请参阅 CHANGELOG 了解最近更改了什么。
贡献
请参阅CONTRIBUTING以获取详细信息。
安全漏洞
请审阅我们的安全策略,了解如何报告安全漏洞。
致谢
许可证
MIT许可(MIT)。请参阅许可文件以获取更多信息。