PHP code example of optix / draftable

1. Go to this page and download the library: Download optix/draftable library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

optix / draftable example snippets


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

    class Post extends Model
    {
        use Draftable;
    }
    

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

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

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

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

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

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

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

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

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

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

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