GithubHelp home page GithubHelp logo

jaymeh / laravel-publishable Goto Github PK

View Code? Open in Web Editor NEW
21.0 1.0 10.0 80 KB

Toggle the published state of your Eloquent models easily

License: MIT License

PHP 100.00%
laravel eloquent trait publishable publishing

laravel-publishable's Introduction

Publishable

Toggle the published state of your Eloquent models easily.

Installation

You can install the package via composer:

composer require pawelmysior/laravel-publishable

Versions

For details about which version of this package to use with your Laravel version please see the table below:

Laravel Version Package Version
<9.x 1.x
9.x 2.x
10.x 3.x
11.x 4.x

Preparation

To start you need to add a published_at nullable timestamp column to your table.

Put this in your table migration:

$table->timestamp('published_at')->nullable();

Now use the trait on the model

<?php
 
namespace App;
  
use Illuminate\Database\Eloquent\Model;
use PawelMysior\Publishable\Publishable;
 
class Post extends Model
{
    use Publishable;
}

Add Fillable Attribute

In Version 1.x, the published() function handles the update in a way where Laravel expects that the published_at field is fillable. As of Version 2.x, we not use fill functionality so the fillable attribute is no longer required.

Usage

You can now use those features:

// Get only published posts
Post::published()->get();
 
// Get only unpublished posts
Post::unpublished()->get();
 
// Check if the post is published
$post->isPublished();
 
// Check if the post is unpublished
$post->isUnpublished();
 
// Publish the post
$post->publish();
 
// Unpublish the post
$post->unpublish();

// Publish the post without firing model events
$post->publishQuietly();
 
// Unpublish the post without firing model events
$post->unpublishQuietly();

A post is considered published when the published_at is not null and in the past.

A post is considered unpublished when the published_at is null or in the future.

Security

If you discover any security-related issues, please email [email protected] instead of using the issue tracker.

Contributing

Any contributions to this repository are welcomed. Please be aware that we are using conventional commits to assist in self documentation and reduce manual work involved with releases.

License

The MIT License (MIT). Please see License File for more information.

laravel-publishable's People

Contributors

austinw avatar jamiesykescreode avatar jaymeh avatar pawelmysior avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

laravel-publishable's Issues

Automatically make published_at field fillable from the trait

The trait provided in this module relies heavily on adding a new published_at field to migrations and any model that uses it. Therefore it would make sense in the initialise hook that laravel can use to add the published_at field to a list of fillable properties.

Laravel 10

Laravel 10 is due to be released very soon (potentially next week). This package needs to be updated in order to allow compatibility.

Change the PR changelog preset

The preset is currently setup to use one that doesn't quite fit our workflow. This will need to be updated to something like the "standard-changelog" preset.

Configurable Field name

This module currently assumes that you will always be using a published_at field on resources.

We should expose some configuration within this module which will allow you to change it to any field.

Adding published_till

Have you thought about adding a published_till variant as well.

You could schedule a model to be visible only during those two dates and after consider it an "archive"?

Support for Laravel 9

Hi,

I'm interesting in this package to provide functionality for making some models publishable. Just wondering if there are any plans to add Laravel 9 support in a new version? Laravel 9 requires PHP > 8.0 so may hinder any backwards compatability.

Happy to submit a PR with dependency bumps to make this happen but I wanted to check the project scope before putting the work in.

Cheers!

Review whether using mass assignment is a good idea

I've been thinking about a recent change to include published_at as a fillable field but I'm wondering if the best solution for security would be to only update that attribute and recall save functions.

Some things to ponder:

  • Overall security
  • Model events (do they fire?)
  • How do other packages handle this type of functionality?

Move away from Orchestral Database

It seems that the Orchestral Database package is no longer maintained, I've had some experience with Orchestral Testbench in the past but not sure what the difference is.

Ideally I would like to find an alternative to this and upgrade the test suite to utilise it.

Add in a new helper function for migrations

When running a migration, rather than specifically adding a field, it might be nice to have a publishable() migration helper function that allows content to apply the field much more easily. This may also make it much better when introducing new potential fields in later migrations if required.

Allow publishing to no longer fire model events

I've got a project which relies on Events and triggering publish functionality seems to cause an issue with duplicate events firing. For that some new publishQuietly and unpublishQuietly functions would be pretty useful.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.