2017 © Pedro Peláez
 

library draftable

image

optix/draftable

  • Tuesday, July 3, 2018
  • by optix
  • Repository
  • 1 Watchers
  • 2 Stars
  • 21 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 2000 % Grown

The README.md

Eloquent Draftable

Packagist Version GitHub Workflow Status StyleCI, (*1)

Add draftable functionality to your eloquent models., (*2)

Installation

You can install this package via composer., (*3)

composer require optix/eloquent-draftable

Setup

  1. Add a nullable timestamp published_at column to your model's database table., (*4)

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

    class Post extends Model
    {
        use Draftable;
    }
    

Usage

Query scopes, (*6)

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., (*7)

// 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, (*8)

$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., (*9)

Draft a model, (*10)

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

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

Schedule a model to be published, (*11)

$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., (*12)

Get the published status of a model, (*13)

// 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., (*14)

The Versions

03/07 2018

dev-master

9999999-dev

  Sources   Download

MIT

The Requires

 

by Jack Robertson