leko-team / laravel-published
Ready-made solution for publishing an entity
Requires (Dev)
- orchestra/testbench: ^8.13.0
- phpunit/phpunit: ^10.4
README
This package can associate published date with Eloquent models. It provides a simple API to work with.
Installation
The library can be installed via Composer:
composer require leko-team/laravel-published
Configuration
To be able publishing eloquent entities you need:
- Add migration with column
published_at
or assignstatic::PUBLISHED_AT
constant with the name of the column you want to use.
php artisan make:migration add_published_at_column_in_`your-table`_table
Schema::table('your-table', function (Blueprint $table) { $table->timestamp('published_at')->nullable()->index(); });
If you have existing records in your table you maybe want to update them with current date. Or perhaps you want to add current date as default value.
- Add trait
PublishedTrait
to your model.
use PublishedTrait;
Examples
Base
To publish entity:
$review = Review::first(); $review->publish();
Publish by specific datetime:
$review = Review::first(); $review->publish(pass here carbon entity);
To unpublish entity:
$review = Review::first(); $review->unpublish();
Scopes
By default from published entity return only published records. You can change this by applying scope to your Eloquent model.
- notPublished
$review = Review::notPublished()->get();
Returns all not published record with null in published_at
include records that must be published in the future.
- withUnpublished
$review = Review::withUnpublished()->get();
Returns all records.
- withoutPublished
$review = Review::withoutPublished()->get();
Returns all unpublished record with null in published_at
.
Credits
A big thank you to Laravel Package for helping out build package with step by step guide.
License
The MIT License (MIT). Please see License File for more information.