1. Go to this page and download the library: Download hootlex/laravel-moderation library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use Hootlex\Moderation\Moderatable;
class Post extends Model
{
use Moderatable;
...
}
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');
});
}
}
//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();
//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();
//it will return all Posts
Post::withAnyStatus()->get();
//it will return all Posts where title is Horse
Post::withAnyStatus()->where('title', 'Horse')->get();
//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();