AppDividend
Latest Code Tutorials

Laravel Moderation Tutorial

2 915

Get real time updates directly on you device, subscribe now.

Laravel Moderation Tutorial is the topic, we will discuss today. We will use hootlex/laravel-moderation Package that allows you to Approve or Reject resources like posts, comments, users, etc. It keeps your application pure by preventing annoying, irrelevant, or abusive content. If You get more information then Go To Github

Laravel Moderation Tutorial

We are going to Configure Laravel Project.

Step 1: Install Laravel Project

Download New Laravel Project by the writing following command.

composer create-project --prefer-dist laravel/laravel laravelmoderation

Step 2: Install Moderation Package

So now let’s install hootlex/laravel-moderation Package in our laravel application.

composer require hootlex/laravel-moderation

Step 3: Define providers

Find the providers in config >> app.php file and register the ModerationServiceProvider.

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

Next, publish the config file.

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

Step 4: Setup a MySQL database

Now, established the database in the .env file.

//.env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=moderation
DB_USERNAME=root
DB_PASSWORD=

I have set local database credentials.

Step 5: Create a model and migration file

Type the following command in your terminal.

php artisan make:model Post -m

It will create two files.

  1. Post.php model.
  2. create__posts_table migration file.

We need to create Schema for the post table. So navigate to Laravel  >>  database  >>  migrations  >> create__posts_table

Related Posts
1 of 33
//create_posts_table
 
public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->string('description');
            $table->smallInteger('status')->default(0);
            $table->dateTime('moderated_at')->nullable();
            $table->timestamps();
        });
    }

In the database, you can see the post table.

Step 6: Config Model

To enable moderation for a model, use the Hootlex\Moderation\Moderatable trait on the model.

use Hootlex\Moderation\Moderatable;

class Post extends Model
{
    use Moderatable;
}

Step 7: Create a view file

Create a file in resources  >>  views  >>   createpost.blade.php and put this following code in it.

<!-- createpost.blade.php -->

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Laravel Moderation Tutorial</title>
    <link rel="stylesheet" href="{{asset('css/app.css')}}">
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">  
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>  
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>  
  </head>
  <body>
    <div class="container">
      <h2>Laravel Moderation Tutorial</h2><br/>
      <div class="container">
    @if(\Session::has('success'))
        <div class="alert alert-success">
            {{\Session::get('success')}}
        </div>
    @endif
    </div>
      <form method="post" action="{{url('post')}}">
        @csrf
        <div class="row">
          <div class="col-md-4"></div>
          <div class="form-group col-md-4">
            <label for="Title">Title:</label>
            <input type="text" class="form-control" name="title">
          </div>
        </div>
        <div class="row">
          <div class="col-md-4"></div>
          <div class="form-group col-md-4">
            <label for="Description">Description:</label>
            <textarea class="form-control" name="description" rows="5"></textarea>
          </div>
        </div>
        <div class="row">
          <div class="col-md-4"></div>
          <div class="form-group col-md-4">
            <button type="submit" class="btn btn-success">Submit</button>
          </div>
        </div>
      </form>
   </div>
  </body>
</html>

Step 8: Create one controller and route 

php artisan make:controller PostController

It will create a controller file called PostController.php.

We register route in routes  >>  web.php file. So let us do it.

//web.php

Route::get('post','PostController@create');
Route::post('post','PostController@store');
Route::get('postview','PostController@index');
Route::get('edit/{id}','PostController@edit');
Route::post('edit/{id}','PostController@update');

Add code to create() function to display view.

//PostController.php

public function create()
    {
        return view('createpost');
    }

Step 9: Save Data into Database

We need to code the store function to save the data in the database.

//PostController.php

use App\Post;

    public function store(Request $request)
    {
        $post=new Post();
        $post->title=$request->get('title');
        $post->description=$request->get('description');
        $post->save();
        return redirect('post')->with('success', 'Post has been successfully submitted pending for approval');
    }

Laravel Approval System

Step 10: Make an index page to list the Information.

For that, frontend, we need to send the data to the index.blade.php. So, in PostController.php file, we need to write the code to retrieve the data and return it to the index view.

//PostController.php

public function index()
    {
        $posts=\DB::table('posts')->get();
        return view('index',compact('posts'));
    }

In resources>> views create a different blade file called index.blade.php file and place the following code in it.

<!-- index.blade.php -->

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Index Page</title>
    <link rel="stylesheet" href="{{asset('css/app.css')}}">
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">    
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>  
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>  
  </head>
  <body>
    <div class="container">
    <br />
    <table class="table table-striped">
    <thead>
      <tr>
        <th>ID</th>
        <th>Title</th>
        <th>Description</th>
        <th>Status</th>
        <th>Action</th>
      </tr>
    </thead>
    <tbody>
    @foreach($posts as $post)
      <tr>
        <td>{{$post->id}}</td>
        <td>{{$post->title}}</td>
        <td>{{$post->description}}</td>
        <td>
        @if($post->status == 0)
        <span class="label label-primary">Pending</span>
        @elseif($post->status == 1)
        <span class="label label-success">Approved</span>
        @elseif($post->status == 2)
        <span class="label label-danger">Rejected</span>
        @else
        <span class="label label-info">Postponed</span>
       @endif
        </td>
        <td><a href="{{action('PostController@edit', $post->id)}}" class="btn btn-warning">Moderate</a></td>
      </tr>
      @endforeach
    </tbody>
  </table>
  </div>
  </body>
</html>

So, when you type the URL: http://localhost:8000/postview, you can see the page like below image.

 

Laravel Moderation Example

Step 11: Build an edit view for updating the Status

Next step will be to code the edit function in PostController.php file and add the following code to it.

public function edit($id)
    {
        $post = \DB::table('posts')->where('id', $id)->first();
        return view('edit', compact('post', 'id'));
    }

Now, make an edit.blade.php file inside resources  >>  views folder.

<!-- edit.blade.php -->
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title> </title>
    <link rel="stylesheet" href="{{asset('css/app.css')}}">
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">    
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>  
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>  

  </head>
  <body>
    <div class="container">
      <form method="post" action="{{action('PostController@update', $id)}}">
        @csrf
         <div class="row">
          <div class="col-md-4"></div>
            <div class="form-group col-md-4">
                <lable>Approval</lable>
                <select name="approve">
                  <option value="0" @if($post->status==0)selected @endif>Pending</option>
                  <option value="1" @if($post->status==1)selected @endif>Approve</option>
                  <option value="2" @if($post->status==2)selected @endif>Reject</option>
                  <option value="3" @if($post->status==3)selected @endif>Postponed</option> 
                </select>
            </div>
        </div>
        <div class="row">
          <div class="col-md-4"></div>
          <div class="form-group col-md-4">
            <button type="submit" class="btn btn-success" style="margin-top:40px">Update</button>
          </div>
        </div>
      </form>
    </div>
  </body>
</html>

Next step will be to code the update function.

//PostController.php

public function update(Request $request, $id)
    {
        switch($request->get('approve'))
        {
            case 0:
                Post::postpone($id);
                break;
            case 1:
                Post::approve($id);
                break;
            case 2:
                Post::reject($id);
                break;
            case 3:
                Post::postpone($id);
                break;
            default:    
                break;

        }
        return redirect('postview');
    }

 

Laravel Moderation

Final Code of PostController.php looks like below code.

//PostController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Post;

class PostController extends Controller
{
    public function create()
    {
        return view('createpost');
    }
    public function store(Request $request)
    {
        $post=new Post();
        $post->title=$request->get('title');
        $post->description=$request->get('description');
        $post->save();
        return redirect('post')->with('success', 'Post has been successfully submited pending for approval');
    }
    public function index()
    {
        $posts=\DB::table('posts')->get();
        return view('index',compact('posts'));
    }
    public function edit($id)
    {
        $post = \DB::table('posts')->where('id', $id)->first();
        return view('edit', compact('post', 'id'));
    }
    public function update(Request $request, $id)
    {
        switch($request->get('approve'))
        {
            case 0:
                Post::postpone($id);
                break;
            case 1:
                Post::approve($id);
                break;
            case 2:
                Post::reject($id);
                break;
            case 3:
                Post::postpone($id);
                break;
            default:    
                break;

        }
        return redirect('postview');
    }

Finally, Our Laravel Moderation Tutorial is over. Thanks for taking.

2 Comments
  1. Yogesh says

    Hi, Krunal
    Can you make tutorial for how to make module in laravel 5.5 or 5.6
    like user-module, product-module, sells-module.
    thanks in advance.

    1. Krunal says

      Will try my best to do that.

Leave A Reply

Your email address will not be published.