Laravel Moderation Tutorial
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
Content Overview
- 1 Laravel Moderation Tutorial
- 2 Step 1: Install Laravel Project
- 3 Step 2: Install Moderation Package
- 4 Step 3: Define providers
- 5 Step 4: Setup a MySQL database
- 6 Step 5: Create a model and migration file
- 7 Step 6: Config Model
- 8 Step 7: Create a view file
- 9 Step 8: Create one controller and route
- 10 Step 9: Save Data into Database
- 11 Step 10: Make an index page to list the Information.
- 12 Step 11: Build an edit view for updating the Status
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.
- Post.php model.
- create__posts_table migration file.
We need to create Schema for the post table. So navigate to Laravel >> database >> migrations >> create__posts_table
//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'); }
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.
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'); }
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.
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.
Will try my best to do that.
You forgot the migrate command on Step 5. Great tut though!
Such an awesome tutorial. You are amazing, thank you so much.
Krunal,
Great tutorial series! I’ve learned a lot from you and really appreciate the time you take to put these together. Stay awesome!
Krunal,
Thank you for posting this tutorial. I’ll definitely be adding this to my site soon. 🙂
How do I add more methods since they are only 4 methods. for example, notForChildren();
I got a problem when I last updated “Missing argument 2 for Illuminate \ Database \ Eloquent \ Model :: setAttribute (). What can I do to resolve the problem?
Hi Krunal,
This is awesome tutorial and thank you for it.
I just need some help, can you tell me how can I make a page for approved posts, so when post is approved to display on that page? And if I reject post to automatically delete it from ‘postview’ page.
Thank you!