{"id":17161184,"url":"https://github.com/lemaur/eloquent-publishing","last_synced_at":"2025-08-21T00:31:09.723Z","repository":{"id":44806353,"uuid":"331398266","full_name":"leMaur/eloquent-publishing","owner":"leMaur","description":"Easily make your eloquent model publishable.","archived":false,"fork":false,"pushed_at":"2024-11-06T09:31:57.000Z","size":66,"stargazers_count":20,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-12-09T22:35:27.070Z","etag":null,"topics":["eloquent","eloquent-models","eloquent-publishing","laravel","lemaur","models","publish","publishing"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/leMaur.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":".github/SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"lemaur"}},"created_at":"2021-01-20T18:39:53.000Z","updated_at":"2024-11-17T21:58:35.000Z","dependencies_parsed_at":"2022-09-19T05:12:03.814Z","dependency_job_id":"5987d3c4-1350-49be-9df6-e39f1b0a3d6f","html_url":"https://github.com/leMaur/eloquent-publishing","commit_stats":{"total_commits":27,"total_committers":4,"mean_commits":6.75,"dds":0.4814814814814815,"last_synced_commit":"30c16dc7d3ebf35b4d038269b55854a828028865"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leMaur%2Feloquent-publishing","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leMaur%2Feloquent-publishing/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leMaur%2Feloquent-publishing/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leMaur%2Feloquent-publishing/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/leMaur","download_url":"https://codeload.github.com/leMaur/eloquent-publishing/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230471175,"owners_count":18231193,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["eloquent","eloquent-models","eloquent-publishing","laravel","lemaur","models","publish","publishing"],"created_at":"2024-10-14T22:27:26.400Z","updated_at":"2025-08-21T00:31:09.715Z","avatar_url":"https://github.com/leMaur.png","language":"PHP","funding_links":["https://github.com/sponsors/lemaur","https://github.com/sponsors/leMaur","https://github.com/sponsors/leMaur?frequency=one-time\u0026sponsor=leMaur"],"categories":[],"sub_categories":[],"readme":"# Easily make your eloquent model publishable\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/lemaur/eloquent-publishing.svg?style=flat-square)](https://packagist.org/packages/lemaur/eloquent-publishing)\n[![Total Downloads](https://img.shields.io/packagist/dt/lemaur/eloquent-publishing.svg?style=flat-square)](https://packagist.org/packages/lemaur/eloquent-publishing)\n[![License](https://img.shields.io/packagist/l/lemaur/eloquent-publishing.svg?style=flat-square\u0026color=yellow)](https://github.com/leMaur/eloquent-publishing/blob/main/LICENSE.md)\n[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/lemaur/eloquent-publishing/run-tests.yml?branch=main\u0026label=tests\u0026style=flat-square)](https://github.com/leMaur/eloquent-publishing/actions/workflows/run-tests.yml)\n[![GitHub Sponsors](https://img.shields.io/github/sponsors/lemaur?style=flat-square\u0026color=ea4aaa)](https://github.com/sponsors/leMaur)\n\nThis package provides a trait that will help you publishing eloquent models.\n```php\nuse Lemaur\\Publishing\\Database\\Eloquent\\Publishes;\n\nclass Post extends Model\n{\n    use Publishes;\n}\n```\nIt also includes custom schema builder blueprint methods to help you setting up your migrations with ease.\n\n## Support Me\n\nHey folks,\n\nDo you like this package? Do you find it useful and it fits well in your project?\n\nI am glad to help you, and I would be so grateful if you considered supporting my work.\n\nYou can even choose 😃:\n* You can [sponsor me 😎](https://github.com/sponsors/leMaur) with a monthly subscription.\n* You can [buy me a coffee ☕ or a pizza 🍕](https://github.com/sponsors/leMaur?frequency=one-time\u0026sponsor=leMaur) just for this package.\n* 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. \n* You can \"Star ⭐\" this repository (it's free 😉).\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require lemaur/eloquent-publishing\n```\n\n## Usage\nYour eloquent models should use the `Lemaur\\Publishing\\Database\\Eloquent\\Publishes` trait.\n\nYour migration files should have a field to save the publishing date.\n\nHere's a real-life example of how to implement the trait on a Post model.\n\n[_(jump to all the available methods)_](#available-methods)\n```php\n/** app\\Models\\Post.php */\n\nnamespace App\\Models;\n\nuse Illuminate\\Database\\Eloquent\\Model;\nuse Lemaur\\Publishing\\Database\\Eloquent\\Publishes;\n\nclass Post extends Model\n{\n    use Publishes;\n}\n```\n\n```php\n/** database\\migrations\\create_posts_table.php */\n\nuse Illuminate\\Database\\Migrations\\Migration;\nuse Illuminate\\Database\\Schema\\Blueprint;\nuse Illuminate\\Support\\Facades\\Schema;\n\nclass CreatePostsTable extends Migration\n{\n    public function up()\n    {\n        Schema::create('posts', function (Blueprint $table) {\n            $table-\u003eid();\n            $table-\u003estring('title');\n            $table-\u003estring('slug');\n            $table-\u003elongText('body')-\u003enullable();\n            $table-\u003etimestamps();\n\n            $table-\u003epublishes(); // equivalent to `$table-\u003etimestamp('published_at')-\u003enullable();`\n        });\n    }\n\n    ...\n}\n```\n\n### Available methods\n\n#### Using in your migration files.\n```php\n/** add a nullable timestamp column named \"published_at\"  */\n$table-\u003epublishes();\n\n/** it may accepts a custom column name and an optional precision (total digits) */\n$table-\u003epublishes('published_at', $precision = 0);\n\n/** add a nullable timestampTz column named \"published_at\"  */\n$table-\u003epublishesTz();\n\n/** it may accepts a custom column name and an optional precision (total digits) */\n$table-\u003epublishesTz('published_at', $precision = 0);\n\n/** drop the column named \"published_at\"  */\n$table-\u003edropPublishes();\n\n/** it may accepts a custom column name */\n$table-\u003edropPublishes('published_at');\n\n/** drop the column named \"published_at\"  */\n$table-\u003edropPublishesTz();\n\n/** it may accepts a custom column name */\n$table-\u003edropPublishesTz('published_at');\n```\n\u003e For more information about timestamps, refer to the [Laravel Documentation](https://laravel.com/docs/8.x/migrations#column-method-timestamp)\n\n[_(jump to the customize section)_](#customize)\n\n#### Using in your controllers, actions or whatever you need\n\n```php\n// Publish your model (this set the publish date at the current date time)\n$post-\u003epublish();\n\n// 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)\n$post-\u003epublish(Carbon::parse('tomorrow'));\n\n// Unpublish your model\n$post-\u003eunpublish();\n\n// Check if the model is published (current date time or in the past)\n$bool = $post-\u003eisPublished();\n\n// Check if the model is not published\n$bool = $post-\u003eisNotPublished();\n\n// Check if the model is published with a date time in the future\n$bool = $post-\u003eisPlanned();\n\n// Check if the model is not planned\n$bool = $post-\u003eisNotPlanned();\n\n// Show only published posts\n$onlyPublishedPosts = Post::onlyPublished()-\u003eget();\n\n// Show only planned posts\n$onlyPlannedPosts = Post::onlyPlanned()-\u003eget();\n\n// Show only planned and published posts\n$onlyPlannedAndPublishedPosts = Post::onlyPlannedAndPublished()-\u003eget();\n\n// Show only posts not planned nor published\n$withoutPlannedAndPublishedPosts = Post::withoutPlannedAndPublished()-\u003eget();\n\n// Order by latest published posts\n$latestPublishedPosts = Post::latestPublished()-\u003eget();\n\n// Order by oldest published posts\n$oldestPublishedPosts = Post::oldestPublished()-\u003eget();\n\n// Order by latest planned posts\n$latestPlannedPosts = Post::latestPlanned()-\u003eget();\n\n// Order by oldest planned posts\n$oldestPlannedPosts = Post::oldestPlanned()-\u003eget();\n\n// or you can combine them together...\n\n// Get only published posts ordered by latest published\n$posts = Post::onlyPublished()-\u003elatestPublished()-\u003eget();\n\n// Get only planned posts ordered by latest planned\n$posts = Post::onlyPlanned()-\u003elatestPlanned()-\u003eget();\n\n```\n\n## Customize\n\nIf you want to change the column name, you need to specify it in your model and in your migration file. Let me show you:\n```php\n// in your model\n\nclass Post extends Model\n{\n    use Publishes;\n\n    /**\n     * The custom name of the \"published at\" column.\n     *\n     * @var string\n     */\n    const PUBLISHED_AT = 'publish_date';\n}\n\n// in your migration file\n\nclass CreatePostsTable extends Migration\n{\n    public function up()\n    {\n        Schema::create('posts', function (Blueprint $table) {\n            $table-\u003eid();\n            ...\n            $table-\u003epublishes('publish_date');\n        });\n    }\n\n    ...\n}\n```\n\n## Events\n\nWhen you publish or unpublish a model, the package dispatches several events: `publishing`, `published`, `unpublishing`, `unpublished`.\n\nThe `publishing` / `published` events will dispatch when a model is published.\nThe `unpublishing` / `unpublished` events will dispatch when a model is unpublished.\n\n\u003e For more information about the events, refer to the [Laravel Documentation](https://laravel.com/docs/8.x/eloquent#events)\n\n## Testing\n\n```bash\ncomposer test\n```\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.\n\n## Contributing\n\nPlease see [CONTRIBUTING](CONTRIBUTING.md) for details.\n\n## Security Vulnerability\n\nPlease review [our security policy](../../security/policy) on how to report security vulnerabilities.\n\n## Credits\n\n- [Maurizio](https://github.com/lemaur)\n- [All Contributors](../../contributors)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flemaur%2Feloquent-publishing","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flemaur%2Feloquent-publishing","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flemaur%2Feloquent-publishing/lists"}