leko-team / laravel-published
发布实体的现成解决方案
1.0.0
2023-10-13 16:22 UTC
Requires (Dev)
- orchestra/testbench: ^8.13.0
- phpunit/phpunit: ^10.4
README
此包可以将发布日期与Eloquent模型关联起来。它提供了一个简单的API进行操作。
安装
可以通过Composer安装此库
composer require leko-team/laravel-published
配置
要能够发布Eloquent实体,你需要
- 添加具有列
published_at
的迁移,或将static::PUBLISHED_AT
常量分配为你想要使用的列的名称。
php artisan make:migration add_published_at_column_in_`your-table`_table
Schema::table('your-table', function (Blueprint $table) { $table->timestamp('published_at')->nullable()->index(); });
如果你在你的表中已有现有记录,你可能想用当前日期更新它们。或者你可能想将当前日期作为默认值添加。
- 将特质
PublishedTrait
添加到你的模型中。
use PublishedTrait;
示例
基础
发布实体
$review = Review::first(); $review->publish();
按特定日期时间发布
$review = Review::first(); $review->publish(pass here carbon entity);
取消发布实体
$review = Review::first(); $review->unpublish();
作用域
默认情况下,从已发布实体返回仅包含已发布记录。你可以通过将作用域应用于Eloquent模型来更改此设置。
- notPublished
$review = Review::notPublished()->get();
返回所有未发布的记录,其中 published_at
为 null,包括必须在未来发布的记录。
- withUnpublished
$review = Review::withUnpublished()->get();
返回所有记录。
- withoutPublished
$review = Review::withoutPublished()->get();
返回所有未发布的记录,其中 published_at
为 null。
致谢
非常感谢 Laravel Package 在构建包时的逐步指南。
许可
MIT 许可证 (MIT)。请参阅 许可文件 获取更多信息。