PHPackages                             lemaur/eloquent-publishing - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. lemaur/eloquent-publishing

ActiveLibrary[Database &amp; ORM](/categories/database)

lemaur/eloquent-publishing
==========================

3.3.0(2mo ago)217.4k1MITPHPPHP ^8.1CI passing

Since Jan 23Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/leMaur/eloquent-publishing)[ Packagist](https://packagist.org/packages/lemaur/eloquent-publishing)[ Docs](https://github.com/lemaur/eloquent-publishing)[ GitHub Sponsors](https://github.com/lemaur)[ RSS](/packages/lemaur-eloquent-publishing/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (8)Dependencies (17)Versions (12)Used By (1)

Easily make your eloquent model publishable
===========================================

[](#easily-make-your-eloquent-model-publishable)

[![Latest Version on Packagist](https://camo.githubusercontent.com/9451f45d6115e1cd1813885315a5b32c6a245834f4cc6f8884b58f882bd544b6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6c656d6175722f656c6f7175656e742d7075626c697368696e672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/lemaur/eloquent-publishing)[![Total Downloads](https://camo.githubusercontent.com/648d0f112749f3d669b35a9a29e1816947022e3a7df228c84e3e5436dffb909c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6c656d6175722f656c6f7175656e742d7075626c697368696e672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/lemaur/eloquent-publishing)[![License](https://camo.githubusercontent.com/05ad0a7a92a204002c2ec7da67879dd9d9512d8ed2e0045aa7783fd50d3d463e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6c656d6175722f656c6f7175656e742d7075626c697368696e672e7376673f7374796c653d666c61742d73717561726526636f6c6f723d79656c6c6f77)](https://github.com/leMaur/eloquent-publishing/blob/main/LICENSE.md)[![GitHub Tests Action Status](https://camo.githubusercontent.com/f0635b86c9cf600335561aac38164218dc40fd88e82b279e215bfcd41f2dfb69/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6c656d6175722f656c6f7175656e742d7075626c697368696e672f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/leMaur/eloquent-publishing/actions/workflows/run-tests.yml)[![GitHub Sponsors](https://camo.githubusercontent.com/67130ea26c0cdbb7c18af5c1fd1c7995f000bc3da607abffed1e39d789f48b2f/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73706f6e736f72732f6c656d6175723f7374796c653d666c61742d73717561726526636f6c6f723d656134616161)](https://github.com/sponsors/leMaur)

This package provides a trait that will help you publishing eloquent models.

```
use Lemaur\Publishing\Database\Eloquent\Publishes;

class Post extends Model
{
    use Publishes;
}
```

It also includes custom schema builder blueprint methods to help you setting up your migrations with ease.

Support Me
----------

[](#support-me)

Hey folks,

Do you like this package? Do you find it useful and it fits well in your project?

I am glad to help you, and I would be so grateful if you considered supporting my work.

You can even choose 😃:

- You can [sponsor me 😎](https://github.com/sponsors/leMaur) with a monthly subscription.
- You can [buy me a coffee ☕ or a pizza 🍕](https://github.com/sponsors/leMaur?frequency=one-time&sponsor=leMaur) just for this package.
- You can [plant trees 🌴](https://ecologi.com/lemaur?r=6012e849de97da001ddfd6c9). By using this link we will both receive 30 trees for free and the planet (and me) will thank you.
- You can "Star ⭐" this repository (it's free 😉).

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require lemaur/eloquent-publishing
```

Usage
-----

[](#usage)

Your eloquent models should use the `Lemaur\Publishing\Database\Eloquent\Publishes` trait.

Your migration files should have a field to save the publishing date.

Here's a real-life example of how to implement the trait on a Post model.

[*(jump to all the available methods)*](#available-methods)

```
/** 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();`
        });
    }

    ...
}
```

### Available methods

[](#available-methods)

#### Using in your migration files.

[](#using-in-your-migration-files)

```
/** 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');
```

> For more information about timestamps, refer to the [Laravel Documentation](https://laravel.com/docs/8.x/migrations#column-method-timestamp)

[*(jump to the customize section)*](#customize)

#### Using in your controllers, actions or whatever you need

[](#using-in-your-controllers-actions-or-whatever-you-need)

```
// 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();
```

Customize
---------

[](#customize)

If you want to change the column name, you need to specify it in your model and in your migration file. Let me show you:

```
// 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');
        });
    }

    ...
}
```

Events
------

[](#events)

When you publish or unpublish a model, the package dispatches several events: `publishing`, `published`, `unpublishing`, `unpublished`.

The `publishing` / `published` events will dispatch when a model is published. The `unpublishing` / `unpublished` events will dispatch when a model is unpublished.

> For more information about the events, refer to the [Laravel Documentation](https://laravel.com/docs/8.x/eloquent#events)

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security Vulnerability
----------------------

[](#security-vulnerability)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Maurizio](https://github.com/lemaur)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

54

—

FairBetter than 97% of packages

Maintenance88

Actively maintained with recent releases

Popularity31

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity68

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~235 days

Recently: every ~277 days

Total

9

Last Release

60d ago

Major Versions

1.1.1 → 2.0.02022-01-23

2.0.0 → 3.0.02023-03-08

PHP version history (3 changes)1.0.0PHP ^7.4|^8.0

1.1.1PHP ^8.0

3.0.0PHP ^8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/2696e190d36ed3a6809d954e3ae2153f969be340ef9b188f8a02bdc98b52f6c7?d=identicon)[leMaur](/maintainers/leMaur)

---

Top Contributors

[![leMaur](https://avatars.githubusercontent.com/u/2118799?v=4)](https://github.com/leMaur "leMaur (34 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (24 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (13 commits)")

---

Tags

eloquenteloquent-modelseloquent-publishinglaravellemaurmodelspublishpublishinglaravelmodeleloquentpublishinglemaureloquent-publishing

###  Code Quality

TestsPHPUnit

Static AnalysisRector

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/lemaur-eloquent-publishing/health.svg)

```
[![Health](https://phpackages.com/badges/lemaur-eloquent-publishing/health.svg)](https://phpackages.com/packages/lemaur-eloquent-publishing)
```

###  Alternatives

[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k7.2M71](/packages/mongodb-laravel-mongodb)[tucker-eric/eloquentfilter

An Eloquent way to filter Eloquent Models

1.8k4.8M26](/packages/tucker-eric-eloquentfilter)[dyrynda/laravel-model-uuid

This package allows you to easily work with UUIDs in your Laravel models.

4802.8M8](/packages/dyrynda-laravel-model-uuid)[spiritix/lada-cache

A Redis based, automated and scalable database caching layer for Laravel

591444.8k2](/packages/spiritix-lada-cache)[pdphilip/elasticsearch

An Elasticsearch implementation of Laravel's Eloquent ORM

145360.2k4](/packages/pdphilip-elasticsearch)[highsolutions/eloquent-sequence

A Laravel package for easy creation and management sequence support for Eloquent models with elastic configuration.

121130.3k](/packages/highsolutions-eloquent-sequence)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
