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 this 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 _ ');
// $ 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 with the title Horse approved
Post: where ('title', 'horse')-> get ();
Query models to be reviewed or rejected:
// Return posts to be reviewed
Post: pending ()-> get ();
// Return review rejected posts
Post: rejected ()-> get ();
// Return to approved and to be reviewed posts
Post: withPending ()-> get ();
// Return the posts that pass the review and fail the review
Post: withRejected ()-> get ();
Query all models:
// Return all posts
Post: withAnyStatus ()-> get ();
// Returns all models whose title is Horse.
Post: withAnyStatus ()-> where ('title', 'horse')-> get ();
Model status
There are three helper functions to help us check the model status:
// Check whether the model is awaiting review
$ 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 _ ';
To overwrite the moderated_by field:
Const MODERATED_BY = 'mod _ ';
To enable or disable strict review:
Public static $ strictModeration = true;