lemaur / eloquent-publishing
3.0.1
2023-03-20 08:55 UTC
Requires
- php: ^8.1
- illuminate/support: ^10.0
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^8.0
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-09-08 10:05:55 UTC
README
此包提供了一种特性,可以帮助您发布eloquent模型。
use Lemaur\Publishing\Database\Eloquent\Publishes; class Post extends Model { use Publishes; }
它还包括自定义模式构建蓝图方法,帮助您轻松设置迁移。
支持我
大家好,
你喜欢这个包吗?你发现它有用,并且非常适合你的项目吗?
我很高兴帮助你,如果你考虑支持我的工作,我将不胜感激。
你可以选择 😃
- 你可以通过赞助我 😎 来进行月度订阅。
- 你也可以为我买一杯咖啡 ☕ 或一份披萨 🍕 仅为此包。
- 你还可以种树 🌴。使用此链接,我们都会免费获得30棵树,地球(以及我)都会感谢你。
- 你可以“Star ⭐”此仓库(免费哦 😉)。
安装
您可以通过composer安装此包。
composer require lemaur/eloquent-publishing
用法
您的eloquent模型应使用Lemaur\Publishing\Database\Eloquent\Publishes
特性。
您的迁移文件应有一个字段来保存发布日期。
以下是一个在Post模型上实现特性的实际示例。
/** app\Models\Post.php */ namespace App\Models; use Illuminate\Database\Eloquent\Model; use Lemaur\Publishing\Database\Eloquent\Publishes; class Post extends Model { use Publishes; }
/** database\migrations\create_posts_table.php */ use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreatePostsTable extends Migration { public function up() { Schema::create('posts', function (Blueprint $table) { $table->id(); $table->string('title'); $table->string('slug'); $table->longText('body')->nullable(); $table->timestamps(); $table->publishes(); // equivalent to `$table->timestamp('published_at')->nullable();` }); } ... }
可用方法
在迁移文件中使用。
/** add a nullable timestamp column named "published_at" */ $table->publishes(); /** it may accepts a custom column name and an optional precision (total digits) */ $table->publishes('published_at', $precision = 0); /** add a nullable timestampTz column named "published_at" */ $table->publishesTz(); /** it may accepts a custom column name and an optional precision (total digits) */ $table->publishesTz('published_at', $precision = 0); /** drop the column named "published_at" */ $table->dropPublishes(); /** it may accepts a custom column name */ $table->dropPublishes('published_at'); /** drop the column named "published_at" */ $table->dropPublishesTz(); /** it may accepts a custom column name */ $table->dropPublishesTz('published_at');
有关时间戳的更多信息,请参阅Laravel 文档
在控制器、动作或您需要的地方使用。
// Publish your model (this set the publish date at the current date time) $post->publish(); // Publish your model with custom date time (can be in the future or in the past, as you wish. It accepts a class that implement \DatetimeInterface) $post->publish(Carbon::parse('tomorrow')); // Unpublish your model $post->unpublish(); // Check if the model is published (current date time or in the past) $bool = $post->isPublished(); // Check if the model is not published $bool = $post->isNotPublished(); // Check if the model is published with a date time in the future $bool = $post->isPlanned(); // Check if the model is not planned $bool = $post->isNotPlanned(); // Show only published posts $onlyPublishedPosts = Post::onlyPublished()->get(); // Show only planned posts $onlyPlannedPosts = Post::onlyPlanned()->get(); // Show only planned and published posts $onlyPlannedAndPublishedPosts = Post::onlyPlannedAndPublished()->get(); // Show only posts not planned nor published $withoutPlannedAndPublishedPosts = Post::withoutPlannedAndPublished()->get(); // Order by latest published posts $latestPublishedPosts = Post::latestPublished()->get(); // Order by oldest published posts $oldestPublishedPosts = Post::oldestPublished()->get(); // Order by latest planned posts $latestPlannedPosts = Post::latestPlanned()->get(); // Order by oldest planned posts $oldestPlannedPosts = Post::oldestPlanned()->get(); // or you can combine them together... // Get only published posts ordered by latest published $posts = Post::onlyPublished()->latestPublished()->get(); // Get only planned posts ordered by latest planned $posts = Post::onlyPlanned()->latestPlanned()->get();
自定义
如果您想更改列名,您需要在模型和迁移文件中指定它。让我给你看看
// in your model class Post extends Model { use Publishes; /** * The custom name of the "published at" column. * * @var string */ const PUBLISHED_AT = 'publish_date'; } // in your migration file class CreatePostsTable extends Migration { public function up() { Schema::create('posts', function (Blueprint $table) { $table->id(); ... $table->publishes('publish_date'); }); } ... }
事件
当您发布或取消发布模型时,此包会触发几个事件:publishing
、published
、unpublishing
、unpublished
。
publishing
/ published
事件将在模型发布时触发。当模型取消发布时,将触发 unpublishing
/ unpublished
事件。
有关事件的更多信息,请参阅Laravel 文档
测试
composer test
更新日志
请参阅更新日志以获取有关最近更改的更多信息。
贡献
请参阅贡献指南以获取详细信息。
安全漏洞
请参阅我们的安全策略了解如何报告安全漏洞。
致谢
许可
MIT 许可证(MIT)。请参阅许可文件以获取更多信息。