定制 optix/eloquent-draftable 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

optix/eloquent-draftable

最新稳定版本:v2.1.0

Composer 安装命令:

composer require optix/eloquent-draftable

包简介

Add draftable functionality to your eloquent models.

README 文档

README

Packagist Version GitHub Workflow Status StyleCI

Add draftable functionality to your eloquent models.

Installation

You can install this package via composer.

composer require optix/eloquent-draftable

Setup

  1. Add a nullable timestamp published_at column to your model's database table.

    $table->timestamp('published_at')->nullable();
  2. Include the Optix\Draftable\Draftable trait in your model.

    class Post extends Model
    {
        use Draftable;
    }

Usage

Query scopes

When the Draftable trait is included in a model, a global scope will be registered to automatically exclude draft records from query results. Therefore, in order to query draft records you must apply one of the local scopes outlined below.

// Only retrieve published records...
$onlyPublished = Post::all();

// Retrieve draft & published records...
$withDrafts = Post::withDrafts()->get();

// Only retrieve draft records...
$onlyDrafts = Post::onlyDrafts()->get();

Publish a model

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

// Publish without saving...
$post->setPublished(true);

// Publish and save...
$post->publish(); // or $post->publish(true);

When you attempt to publish a model that's already been published, the published_at timestamp will not be updated.

Draft a model

// Draft without saving...
$post->setPublished(false);

// Draft and save...
$post->draft(); // or $post->publish(false);

Schedule a model to be published

$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);

The methods outlined above both require a $date parameter of type DateTimeInterface|string|null.

Get the published status of a model

// Determine if the model is published...
$post->isPublished();

// Determine if the model is draft...
$post->isDraft();

License

This package is licensed under the MIT license.

统计信息

  • 总下载量: 2.96k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 28
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 28
  • Watchers: 1
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-03-09