Use LaravelModeration extension to implement a simple content review system in Laravel applications

Source: Internet
Author: User
Use LaravelModeration extension to implement a simple content review system in Laravel applications 1. Introduction

Laravel Moderation is based on Laravel 5. * The implemented simple content review system allows or rejects requests to application resources, such as articles, comments, and users, in order to keep the app Pure by blocking content that is offensive, offensive, or insulting. Simply put, it is to judge whether the resource passes the review by a review status, and then integrate this function into the Laravel Moderation extension package. The general procedure is as follows:

  • First, the user creates resources (articles, comments, or other Eloquent models)
  • After the resource is created, it is in the pending or invisible state (for example, only approved articles are returned through Post: all)
  • The background administrator decides whether to pass or reject the resource.
  • Keep the app clean under the administrator's review
2. Installation

Install with Composer:

composer require hootlex/laravel-moderation

Register a service provider in config/app. php:

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

Finally, release the extension configuration file to the config Directory:

php artisan vendor:publish --provider="Hootlex\Moderation\ModerationServiceProvider" --tag=config
3. model settings

To enable model review, you can use Hootlex \ Moderation \ Moderatable trait in the model class and add the status, moderated_by, and moderated_at fields to the corresponding data table of the model.

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

Create a database migration file:

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 (); // If you want to track who has reviewed the model, add a 'moderated _ by' field // $ 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-> integer ('moderated _ by')-> nullable () -> unsigned ();});}}

Run the migration command to add the corresponding field to the data table posts:

php artisan migrate
4. Use cases

Note: The Post model is used as an example to demonstrate the Post model, and so on. you can review any model.

Audit model

Review the model by id (pass or fail ):

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

Or query the builder:

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

By default, only approved models are returned:

// Return all approved models Post: all (); // return the model Post: where ('title', 'horse ') -> get ();

Query models to be reviewed or rejected:

// Return postsPost: pending ()-> get (); // return the result that the review fails. postsPost: rejected ()-> get (); // return the postsPost: withPending ()-> get (); // return the postsPost: withRejected () -> get ();

Query all models:

// Return all postsPost: withAnyStatus ()-> get (); // return all models with Horse title: withAnyStatus ()-> where ('title ', 'horse')-> get ();
Model status

There are three helper functions to help us check the model status:

// Check whether the model is to be reviewed $ post-> isPending (); // check whether the model is approved $ post-> isApproved (); // check whether the model is not approved. $ post-> isRejected ();
Strict review

Strict review means that resources can be queried only after the review is passed. To return to a model in another state, you must modify the configuration to disable strict review. let's take a look at how to configure it.

5. configuration Global configuration

You can configure global settings by editing the config/moderation. php file. in this file, you can configure the following options:

  • Status_column indicates the status field of the default status field in the data table.
  • Moderated_at_column indicates the default field moderated_at in the data table.
  • Moderated_by_column indicates the default field moderated_by in the data table.
  • Strict indicates strict review
Model configuration

In the model, you can define some variables to overwrite the global configuration.

For example, to overwrite the status field, you can do this:

const MODERATION_STATUS = 'moderation_status';

To overwrite the moderated_at field:

const MODERATED_AT = 'mod_at';

To overwrite the moderated_by field:

const MODERATED_BY = 'mod_by';

To enable or disable strict review:

public static $strictModeration = true;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.