bmatovu/laravel-publishable 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

bmatovu/laravel-publishable

Composer 安装命令:

composer require bmatovu/laravel-publishable

包简介

Laravel publisable.

README 文档

README

Build Status Scrutinizer Code Quality Code Coverage StyleCI Documentation

This package contains a trait to make Eloquent models publishable. It enables the model to hold a published vs non-published state, which comes in handy for things like blog posts that can be drafts or final (published) posts.

It uses a published_at attribute to determine the model state ie, if the model published_at is null, the model isn't published.

Installation

Install via Composer package manager:

composer require bmatovu/laravel-publishable

Usage

Add the publsihed_at column to your database 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->timestamp('published_at')->nullable();
        });
    }
}

To make a model publishable, use the Bmatovu\Publishable\Publishable trait on the model:

<?php

namespace App\Models;

use Bmatovu\Publishable\Publishable;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use Publishable;
}

{tip} The Publishable trait will automatically cast the published_at attribute to a DateTime / Carbon instance for you.

Now, when you call the publish method on the model, the published_at column will be set to the current date and time.

Querying Publishable Models

When querying a model that is publishable, the unpublished models will automatically be excluded from all query results.

$publishedPosts = Post::get();

$publishedPosts = Post::onlyPublished()->get();

However, you may force unpublished models to appear in a result set using the withDrafts method on the query:

$posts = Posts::withDrafts()->get();

You may also retrieve only unpublished models using the onlyDrafts method.

$drafts = Posts::onlyDrafts()->get();

To determine if a given model instance has been published, use the isPublished method:

if ($post->isPublished()) {
    // ...
}

Publishing Models

You can save a model as published in your database like;

// Publishing a single model instance...
$post->publish();

// Publishing all related models...
$post->inLifeStyle()->publish();

Unpublishing Models

You can "un-published" a published model like;

$post->unpublish();

统计信息

  • 总下载量: 40
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 0
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-06-23

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固