optix / draftable
v2.1.0
2020-10-13 09:55 UTC
Requires
- php: ^7.3
- illuminate/database: ~5.8.0|^6.0|^7.0|^8.0
Requires (Dev)
- orchestra/testbench: ~3.8.0|^4.0|^5.0|^6.0
This package is auto-updated.
Last update: 2020-10-13 09:56:26 UTC
README
为您的eloquent模型添加草稿功能。
安装
您可以通过composer安装此包。
composer require optix/eloquent-draftable
设置
-
将可空的时间戳列
published_at添加到您的模型数据库表。$table->timestamp('published_at')->nullable();
-
在您的模型中包含
Optix\Draftable\Draftable特性。class Post extends Model { use Draftable; }
用法
查询作用域
当在模型中包含 Draftable 特性时,将注册一个全局作用域以自动排除查询结果中的草稿记录。因此,要查询草稿记录,您必须应用以下列出的本地作用域之一。
// Only retrieve published records... $onlyPublished = Post::all(); // Retrieve draft & published records... $withDrafts = Post::withDrafts()->get(); // Only retrieve draft records... $onlyDrafts = Post::onlyDrafts()->get();
发布模型
$post = Post::withDrafts()->first(); // Publish without saving... $post->setPublished(true); // Publish and save... $post->publish(); // or $post->publish(true);
当尝试发布已经发布的模型时,不会更新 published_at 时间戳。
草稿模型
// Draft without saving... $post->setPublished(false); // Draft and save... $post->draft(); // or $post->publish(false);
安排模型发布
$publishDate = Carbon::now()->addWeek(); // $publishDate = '2020-01-01 00:00:00'; // $publishDate = '+1 week'; // Schedule without saving... $post->setPublishedAt($publishDate); // Schedule and save... $post->publishAt($publishDate);
上述方法都需要一个类型为 DateTimeInterface|string|null 的 $date 参数。
获取模型的发布状态
// Determine if the model is published... $post->isPublished(); // Determine if the model is draft... $post->isDraft();
许可证
此包根据MIT许可证授权。