Skip to content

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

License

Notifications You must be signed in to change notification settings

gecche/laravel-moderation

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Laravel Moderation Build Status Version Software License

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

Keep your application pure by preventing offensive, irrelevant, or insulting content.

##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. Admin/Moderator/etc decides if the resource will be approved or rejected.

  4. Approved: Resource is now public and queryable.

  5. Rejected: Resource will be excluded from all queries. Rejected resources will be returned only if you scope a query to include them. (scope: withRejected)

  6. You application is clean.

##Installation

First, install the package through Composer.

composer require hootlex/laravel-moderation

Then include the service provider inside config/app.php.

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

Lastly you publish the config file.

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

Setup the Model(s)

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

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

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

Example Migration:

class AddModeratioColumnsToPostsTable 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();
        });
    }

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

You are ready to go!

##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.

###Moderate Models You can moderate a model by referencing it's id.

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

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

Or by making a query.

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

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

###Query Models By default only Approved models will be returned on queries. To change this behavior check the configuration.

#####To query the Approved Posts, run your queries as always.

//it will return all Approved Posts
Post::all();

//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 Approved and Pending Posts
Post::withPending()->get();

//it will return Approved and Rejected Posts
Post::withRejected()->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.

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

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

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

##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.

##Configuration

###Global Configuration To configuration Moderation package globally you have to edit config/moderation.php. Inside moderation.php you can configure the following:

  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. strict represents Strict Moderation.

###Model Configuration Inside your Model you can define some variables to overwrite Global Settings.

To overwrite status column define:

const MODERATION_STATUS = 'moderation_status';

To overwrite moderated_at column define:

const MODERATED_AT = 'mod_at';

To enable or disable Strict Moderation:

public static $strictModeration = true;

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%