2017 © Pedro Peláez
 

library laravel-moderation

A simple Content Moderation System for Laravel 5.* that allows you to Approve or Reject resources like posts, comments, users, etc.

image

hootlex/laravel-moderation

A simple Content Moderation System for Laravel 5.* that allows you to Approve or Reject resources like posts, comments, users, etc.

  • Friday, June 8, 2018
  • by Hootlex
  • Repository
  • 11 Watchers
  • 348 Stars
  • 19,864 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 45 Forks
  • 7 Open issues
  • 16 Versions
  • 12 % Grown

The README.md

Laravel Moderation Build Status Version Total Downloads Software License

A simple Moderation System for Laravel 5.* that allows you to Approve or Reject resources like posts, comments, users, etc., (*1)

Keep your application pure by preventing offensive, irrelevant, or insulting content., (*2)

Possible Use Case

  1. User creates a resource (a post, a comment or any Eloquent Model).
  2. The resource is pending and invisible in website (ex. Post::all() returns only approved posts).
  3. Moderator decides if the resource will be approved, rejected or postponed., (*3)

    1. Approved: Resource is now public and queryable.
    2. Rejected: Resource will be excluded from all queries. Rejected resources will be returned only if you scope a query to include them. (scope: withRejected)
    3. Postponed: Resource will be excluded from all queries until Moderator decides to approve it.
  4. You application is clean., (*4)

Installation

First, install the package through Composer., (*5)

composer require hootlex/laravel-moderation

If you are using Laravel < 5.5, you need to add Hootlex\Moderation\ModerationServiceProvider to your config/app.php providers array:, (*6)

'providers' => [
    ...
    Hootlex\Moderation\ModerationServiceProvider::class,
    ...
];

Lastly you publish the config file., (*7)

php artisan vendor:publish --provider="Hootlex\Moderation\ModerationServiceProvider" --tag=config

Prepare Model

To enable moderation for a model, use the Hootlex\Moderation\Moderatable trait on the model and add the status, moderated_by and moderated_at columns to your model's table., (*8)

use Hootlex\Moderation\Moderatable;
class Post extends Model
{
    use Moderatable;
    ...
}

Create a migration to add the new columns. (You can use custom names for the moderation columns), (*9)

Example Migration:, (*10)

class AddModerationColumnsToPostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('posts', function (Blueprint $table) {
            $table->smallInteger('status')->default(0);
            $table->dateTime('moderated_at')->nullable();
            //To track who moderated the Model, add 'moderated_by' and set the column name in the config file.
            //$table->integer('moderated_by')->nullable()->unsigned();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('posts', function(Blueprint $table)
        {
            $table->dropColumn('status');
            $table->dropColumn('moderated_at');
            //$table->dropColumn('moderated_by');
        });
    }
}

You are ready to go!, (*11)

Usage

Note: In next examples I will use Post model to demonstrate how the query builder works. You can Moderate any Eloquent Model, even User., (*12)

Moderate Models

You can moderate a model Instance:, (*13)

$post->markApproved();

$post->markRejected();

$post->markPostponed();

$post->markPending();

or by referencing it's id, (*14)

Post::approve($post->id);

Post::reject($post->id);

Post::postpone($post->id);

or by making a query., (*15)

Post::where('title', 'Horse')->approve();

Post::where('title', 'Horse')->reject();

Post::where('title', 'Horse')->postpone();

Query Models

By default only Approved models will be returned on queries. To change this behavior check the configuration., (*16)

To query the Approved Posts, run your queries as always.
//it will return all Approved Posts (strict mode)
Post::all();

// when not in strict mode
Post::approved()->get();

//it will return Approved Posts where title is Horse
Post::where('title', 'Horse')->get();

Query pending or rejected models.
//it will return all Pending Posts
Post::pending()->get();

//it will return all Rejected Posts
Post::rejected()->get();

//it will return all Postponed Posts
Post::postponed()->get();

//it will return Approved and Pending Posts
Post::withPending()->get();

//it will return Approved and Rejected Posts
Post::withRejected()->get();

//it will return Approved and Postponed Posts
Post::withPostponed()->get();
Query ALL models
//it will return all Posts
Post::withAnyStatus()->get();

//it will return all Posts where title is Horse
Post::withAnyStatus()->where('title', 'Horse')->get();

Model Status

To check the status of a model there are 3 helper methods which return a boolean value., (*17)

//check if a model is pending
$post->isPending();

//check if a model is approved
$post->isApproved();

//check if a model is rejected
$post->isRejected();

//check if a model is rejected
$post->isPostponed();

Strict Moderation

Strict Moderation means that only Approved resource will be queried. To query Pending resources along with Approved you have to disable Strict Moderation. See how you can do this in the configuration., (*18)

Configuration

Global Configuration

To configuration Moderation package globally you have to edit config/moderation.php. Inside moderation.php you can configure the following:, (*19)

  1. status_column represents the default column 'status' in the database.
  2. moderated_at_column represents the default column 'moderated_at' in the database.
  3. moderated_by_column represents the default column 'moderated_by' in the database.
  4. strict represents Strict Moderation.

Model Configuration

Inside your Model you can define some variables to overwrite Global Settings., (*20)

To overwrite status column define:, (*21)

const MODERATION_STATUS = 'moderation_status';

To overwrite moderated_at column define:, (*22)

const MODERATED_AT = 'mod_at';

To overwrite moderated_by column define:, (*23)

const MODERATED_BY = 'mod_by';

To enable or disable Strict Moderation:, (*24)

public static $strictModeration = true;

The Versions

08/06 2018

dev-master

9999999-dev

A simple Content Moderation System for Laravel 5.* that allows you to Approve or Reject resources like posts, comments, users, etc.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Alex Kyriakidis

laravel moderation moderation-system content-moderation

08/06 2018

v1.1.0

1.1.0.0

A simple Content Moderation System for Laravel 5.* that allows you to Approve or Reject resources like posts, comments, users, etc.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Alex Kyriakidis

laravel moderation moderation-system content-moderation

16/07 2017

v1.0.12

1.0.12.0

A simple Content Moderation System for Laravel 5.* that allows you to Approve or Reject resources like posts, comments, users, etc.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Alex Kyriakidis

laravel moderation moderation-system content-moderation

29/05 2017

v1.0.11

1.0.11.0

A simple Content Moderation System for Laravel 5.* that allows you to Approve or Reject resources like posts, comments, users, etc.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Alex Kyriakidis

laravel moderation moderation-system content-moderation

12/02 2017

v1.0.10

1.0.10.0

A simple Content Moderation System for Laravel 5.* that allows you to Approve or Reject resources like posts, comments, users, etc.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Alex Kyriakidis

laravel moderation moderation-system content-moderation

10/02 2017

v1.0.9

1.0.9.0

A simple Content Moderation System for Laravel 5.* that allows you to Approve or Reject resources like posts, comments, users, etc.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Alex Kyriakidis

laravel moderation moderation-system content-moderation

26/08 2016

v1.0.8

1.0.8.0

A simple Content Moderation System for Laravel 5.* that allows you to Approve or Reject resources like posts, comments, users, etc.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Alex Kyriakidis

laravel moderation moderation-system content-moderation

22/06 2016

v1.0.7

1.0.7.0

A simple Content Moderation System for Laravel 5.* that allows you to Approve or Reject resources like posts, comments, users, etc.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Alex Kyriakidis

laravel moderation moderation-system content-moderation

22/06 2016

v1.0.6

1.0.6.0

A simple Content Moderation System for Laravel 5.* that allows you to Approve or Reject resources like posts, comments, users, etc.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Alex Kyriakidis

laravel moderation moderation-system content-moderation

22/06 2016

dev-instance-methods

dev-instance-methods

A simple Content Moderation System for Laravel 5.* that allows you to Approve or Reject resources like posts, comments, users, etc.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Alex Kyriakidis

laravel moderation moderation-system content-moderation

06/03 2016

v1.0.5

1.0.5.0

A simple Content Moderation System for Laravel 5.* that allows you to Approve or Reject resources like posts, comments, users, etc.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Alex Kyriakidis

laravel moderation moderation-system content-moderation

05/03 2016

v1.0.4

1.0.4.0

A simple Content Moderation System for Laravel 5.* that allows you to Approve or Reject resources like posts, comments, users, etc.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Alex Kyriakidis

laravel moderation moderation-system content-moderation

13/02 2016

v1.0.3

1.0.3.0

A simple Content Moderation System for Laravel 5.* that allows you to Approve or Reject resources like posts, comments, users, etc.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Alex Kyriakidis

laravel moderation moderation-system content-moderation

05/02 2016

v1.0.2

1.0.2.0

A simple Content Moderation System for Laravel 5.* that allows you to Approve or Reject resources like posts, comments, users, etc.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Alex Kyriakidis

laravel moderation moderation-system content-moderation

29/01 2016

v1.0.1

1.0.1.0

A simple Content Moderation System for Laravel 5.* that allows you to Approve or Reject resources like posts, comments, users, etc.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Alex Kyriakidis

laravel moderation moderation-system content-moderation

29/01 2016

v1.0.0

1.0.0.0

A simple Content Moderation System for Laravel 5.* that allows you to Approve or Reject resources like posts, comments, users, etc.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alex Kyriakidis

laravel moderation moderation-system content-moderation