novius/laravel-publishable

A Laravel Eloquent model trait for publishable resource

2.0.0 2023-07-10 15:56 UTC

This package is auto-updated.

Last update: 2024-04-26 10:25:10 UTC


README

Novius CI Packagist Release License: AGPL v3

Introduction

A package for making Laravel Eloquent models "publishable" using 4 states : draft, published, unpublished and scheduled. Manage an additional published_first_at date for order by and display.

Requirements

  • Laravel 8.0, 9.0 or 10.0

Installation

You can install the package via composer:

composer require novius/laravel-publishable
php artisan vendor:publish --provider="Novius\Publishable\LaravelPublishableServiceProvider" --tag=lang

Usage

Migrations

Schema::create('posts', function (Blueprint $table) {
    $table->id();
    $table->string('title');
    $table->text('text');
    $table->timestamps();
    $table->publishable(); // Macro provided by the package
});

Eloquent Model Trait

namespace App\Models;

use \Illuminate\Database\Eloquent\Model;
use \Novius\LaravelPublishable\Publishable;

class Post extends Model {
    use Publishable;
    ...
}

Extensions

The extensions shipped with this trait include; notPublished, published, onlyDrafted, onlyExpired, onlyWillBePublished and can be used accordingly:

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

$postsPublished = Post::all();
$postsPublished = Post::query()->published();
$onlyNotPublishedPosts = Post::query()->notPublished();
$onlyDraftedPosts = Post::query()->onlyDrafted();
$onlyExpiredPosts = Post::query()->onlyExpired();
$onlyWillBePublishedPosts = Post::query()->onlyWillBePublished();

When not specifing any additional scopes, all not published models are excluded from the query by default to prevent leaks of not published data.

Testing

composer run test

CS Fixer

Lint your code with Laravel Pint using:

composer run cs-fix

Licence

This package is under GNU Affero General Public License v3 or (at your option) any later version.