pawelmysior/laravel-publishable

轻松切换 Eloquent 模型的发布状态

4.0.0 2024-03-13 08:47 UTC

This package is auto-updated.

Last update: 2024-09-13 09:56:10 UTC


README

轻松切换 Eloquent 模型的发布状态。

安装

您可以通过 composer 安装此包

composer require pawelmysior/laravel-publishable

版本

有关使用此包的 Laravel 版本的详细信息,请参阅下表

准备

首先,您需要将一个 published_at 可空的时间戳列添加到您的表中。

在您的表迁移中添加以下内容

$table->timestamp('published_at')->nullable();

现在在模型上使用该特性

<?php
 
namespace App;
  
use Illuminate\Database\Eloquent\Model;
use PawelMysior\Publishable\Publishable;
 
class Post extends Model
{
    use Publishable;
}

添加 Fillable 属性

在 1.x 版本中,published() 函数以 Laravel 预期 published_at 字段是可填充的方式来处理更新。从 2.x 版本开始,我们不使用填充功能,因此不再需要 fillable 属性。

用法

现在您可以使用以下功能

// Get only published posts
Post::published()->get();
 
// Get only unpublished posts
Post::unpublished()->get();
 
// Check if the post is published
$post->isPublished();
 
// Check if the post is unpublished
$post->isUnpublished();
 
// Publish the post
$post->publish();
 
// Unpublish the post
$post->unpublish();

// Publish the post without firing model events
$post->publishQuietly();
 
// Unpublish the post without firing model events
$post->unpublishQuietly();

published_at 不是 null 且在过去时,帖子被认为是已发布的。

published_at 是 null 或在将来时,帖子被认为是未发布的。

安全

如果您发现任何安全相关的问题,请通过电子邮件 security@jaymeh.co.uk 联系我们,而不是使用问题跟踪器。

贡献

我们欢迎对存储库的任何贡献。请注意,我们正在使用 conventional commits 来帮助自我文档化和减少发布中涉及的手动工作。

许可

MIT 许可证 (MIT)。有关更多信息,请参阅 许可文件