PHP code example of lemaur / eloquent-publishing

1. Go to this page and download the library: Download lemaur/eloquent-publishing 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/ */

    

lemaur / eloquent-publishing example snippets


use Lemaur\Publishing\Database\Eloquent\Publishes;

class Post extends Model
{
    use Publishes;
}

/** app\Models\Post.php */

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Lemaur\Publishing\Database\Eloquent\Publishes;

class Post extends Model
{
    use Publishes;
}

/** database\migrations\create_posts_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->id();
            $table->string('title');
            $table->string('slug');
            $table->longText('body')->nullable();
            $table->timestamps();

            $table->publishes(); // equivalent to `$table->timestamp('published_at')->nullable();`
        });
    }

    ...
}

/** add a nullable timestamp column named "published_at"  */
$table->publishes();

/** it may accepts a custom column name and an optional precision (total digits) */
$table->publishes('published_at', $precision = 0);

/** add a nullable timestampTz column named "published_at"  */
$table->publishesTz();

/** it may accepts a custom column name and an optional precision (total digits) */
$table->publishesTz('published_at', $precision = 0);

/** drop the column named "published_at"  */
$table->dropPublishes();

/** it may accepts a custom column name */
$table->dropPublishes('published_at');

/** drop the column named "published_at"  */
$table->dropPublishesTz();

/** it may accepts a custom column name */
$table->dropPublishesTz('published_at');

// Publish your model (this set the publish date at the current date time)
$post->publish();

// Publish your model with custom date time (can be in the future or in the past, as you wish. It accepts a class that implement \DatetimeInterface)
$post->publish(Carbon::parse('tomorrow'));

// Unpublish your model
$post->unpublish();

// Check if the model is published (current date time or in the past)
$bool = $post->isPublished();

// Check if the model is not published
$bool = $post->isNotPublished();

// Check if the model is published with a date time in the future
$bool = $post->isPlanned();

// Check if the model is not planned
$bool = $post->isNotPlanned();

// Show only published posts
$onlyPublishedPosts = Post::onlyPublished()->get();

// Show only planned posts
$onlyPlannedPosts = Post::onlyPlanned()->get();

// Show only planned and published posts
$onlyPlannedAndPublishedPosts = Post::onlyPlannedAndPublished()->get();

// Show only posts not planned nor published
$withoutPlannedAndPublishedPosts = Post::withoutPlannedAndPublished()->get();

// Order by latest published posts
$latestPublishedPosts = Post::latestPublished()->get();

// Order by oldest published posts
$oldestPublishedPosts = Post::oldestPublished()->get();

// Order by latest planned posts
$latestPlannedPosts = Post::latestPlanned()->get();

// Order by oldest planned posts
$oldestPlannedPosts = Post::oldestPlanned()->get();

// or you can combine them together...

// Get only published posts ordered by latest published
$posts = Post::onlyPublished()->latestPublished()->get();

// Get only planned posts ordered by latest planned
$posts = Post::onlyPlanned()->latestPlanned()->get();


// in your model

class Post extends Model
{
    use Publishes;

    /**
     * The custom name of the "published at" column.
     *
     * @var string
     */
    const PUBLISHED_AT = 'publish_date';
}

// in your migration file

class CreatePostsTable extends Migration
{
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->id();
            ...
            $table->publishes('publish_date');
        });
    }

    ...
}