novius/laravel-publishable

Laravel Eloquent 模型 trait,用于可发布资源的发布

3.0.0 2024-07-10 13:50 UTC

This package is auto-updated.

Last update: 2024-09-10 14:15:18 UTC


README

Novius CI Packagist Release License: AGPL v3

简介

一个用于使 Laravel Eloquent 模型通过 4 种状态(草稿、已发布、未发布和计划发布)成为可发布的包。管理额外的 published_first_at 日期以进行排序和显示。

要求

  • Laravel >= 10.0
  • PHP >= 8.2

注意:以下说明适用于 Laravel >= 10.0 和 PHP >= 8.2。如果您使用的是早期版本,请参阅 先前版本的文档

安装

您可以通过 composer 安装此包

composer require novius/laravel-publishable
php artisan vendor:publish --provider="Novius\Publishable\LaravelPublishableServiceProvider" --tag=lang

使用方法

迁移

Schema::create('posts', function (Blueprint $table) {
    $table->id();
    $table->string('title');
    $table->text('text');
    $table->timestamps();
    $table->publishable(); // Macro provided by the package
});

Eloquent 模型 trait

namespace App\Models;

use \Illuminate\Database\Eloquent\Model;
use \Novius\LaravelPublishable\Publishable;

class Post extends Model {
    use Publishable;
    ...
}

扩展

此 trait 包含以下扩展:notPublishedpublishedonlyDraftedonlyExpiredonlyWillBePublished,可按需使用

$post = Post::first();
$post->isPublished();

$postsPublished = Post::all();
$postsPublished = Post::query()->published();
$onlyNotPublishedPosts = Post::query()->notPublished();
$onlyDraftedPosts = Post::query()->onlyDrafted();
$onlyExpiredPosts = Post::query()->onlyExpired();
$onlyWillBePublishedPosts = Post::query()->onlyWillBePublished();

如果没有指定任何额外的范围,则默认情况下会从查询中排除所有未发布的模型,以防止未发布数据的泄露。

测试

composer run test

CS Fixer

使用 Laravel Pint 对您的代码进行代码检查

composer run cs-fix

许可证

此包根据 GNU Affero 通用公共许可证 v3 或(在您的选择下)任何后续版本进行许可。