I have a comment bank application where I want users to be able to make comments, but these have to be verified by an admin before they can be displayed. So I want users to see only verified comments, but I do not know how to accomplish this in Laravel.
In my database unverified comments are denoted by having verified_status = 0
and verified comments are verified_comments = 1.
So I only want users to see comments where verified_status = 1
Routes
// for users
Route::group(['middleware' => ['auth', 'role:user']], function() {
Route::get('/dashboard/myprofile', 'App\Http\Controllers\DashboardController@myprofile')->name('dashboard.myprofile');
Route::get('/user-view', [CommentController::class, 'index'])->name('user-view');
Route::post('save-comment-user', [CommentController::class, 'storeUser']);
Route::get('fetch-comments-user', [CommentController::class, 'fetchCommentUser']);
Route::get('edit-comment-user/{id}', [CommentController::class, 'editUser']);
Route::put('update-comment-user/{id}', [CommentController::class, 'updateUser']);
});
CommentController
public function fetchCommentUser()
{
$comments = Comment::all();
return response()->json([
'comments'=>$comments,
]);
}
public function storeUser(Request $request)
{
$validator = Validator::make($request->all(), [
'comment_body'=>'required',
'first_name'=>'required',
'last_name'=>'required',
'email'=>'required',
'comment_tone'=>'required',
'comment_type'=>'required',
'verified_status'=>'required',
]);
if($validator->fails())
{
return response()->json([
'status'=>400,
'errors'=>$validator->messages()
]);
} else {
$comment = new Comment;
$comment->comment_body = $request->input('comment_body');
$comment->first_name = $request->input('first_name');
$comment->last_name = $request->input('last_name');
$comment->email = $request->input('email');
$comment->comment_tone = $request->input('comment_tone');
$comment->comment_type = $request->input('comment_type');
if ($request->has('verified_status')){
$comment->verified_status = 0;
} else{
$comment->verified_status = 1;
}
$comment->save();
return response()->json([
'status'=>200,
'message'=>'Comment Added Successfully.'
]);
}
}
public function updateUser(Request $request, $id)
{
$validator = Validator::make($request->all(), [
'comment_body'=>'required',
'first_name'=>'required',
'last_name'=>'required',
'email'=>'required',
'comment_tone'=>'required',
'comment_type'=>'required',
'verified_status'=>'required',
]);
if($validator->fails())
{
return response()->json([
'status'=>400,
'errors'=>$validator->messages()
]);
} else {
$comment = comment::find($id);
if($comment)
{
$comment->comment_body = $request->input('comment_body');
$comment->first_name = $request->input('first_name');
$comment->last_name = $request->input('last_name');
$comment->email = $request->input('email');
$comment->comment_tone = $request->input('comment_tone');
$comment->comment_type = $request->input('comment_type');
$comment->verified_status = $request->input('verified_status');
$comment->update();
return response()->json([
'status'=>200,
'message'=>'Comment with id:'.$id. ' Updated Successfully.'
]);
} else {
return response()->json([
'status'=>404,
'message'=>'No Comment Found.'
]);
}
}
}
public function editUser($id)
{
$comment = Comment::find($id);
if($comment)
{
return response()->json([
'status'=>200,
'comment'=> $comment,
]);
} else {
return response()->json([
'status'=>404,
'message'=>'No Comment Found.'
]);
}
}
user-view.blade.php Blade file (what the user sees)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Comment Bank</title>
<meta name="csrf-token" content="{{ csrf_token() }}">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</head>
<body>
<x-app-layout>
<div >
<x-slot name="header">
<h2 >
{{ __('Dashboard') }}
</h2>
</x-slot>
<div >
<div >
<h2>Comment Bank</h2>
</div>
<div id="message"></div>
<div >
<button type="button" id="addNewCommentUser" >Add</button>
</div>
<div >
<table id="Table1" >
<thead>
<tr>
<th scope="col">Message Select</th>
<th scope="col">#</th>
<th scope="col">Comment Body</th>
<th scope="col">First Name</th>
<th scope="col">Last Name</th>
<th scope="col">Email</th>
<th scope="col">Comment Tone</th>
<th scope="col">Comment Type</th>
<th scope="col">Verified Status</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<input id="btnGet" type="button" value="Get Selected"/>
</div>
</div>
<div><textarea id="messageList" rows="10" cols="100">Selection</textarea>
<button type="button" id="copy">Copy</button>
</div>
</div>
<!-- boostrap model -->
<div id="comments-crud-model" aria-hidden="true">
<div >
<div >
<div >
<h4 id="commentsCrudModel"></h4>
</div>
<div >
<ul id="msgList"></ul>
<form action="javascript:void(0)" id="addEditCommentFormUser" name="addEditCommentFormUser"
method="POST">
<input type="hidden" name="id" id="id">
<div >
<label for="name" >Comment Body</label>
<div >
<textarea id="comment_body" name="comment_body" rows="4" cols="10"
placeholder="Enter Comment Body"></textarea>
</div>
</div>
<div >
<label >First Name</label>
<div >
<input type="text" id="first_name" name="first_name"
placeholder="Enter First Name" value="" required="">
</div>
</div>
<div >
<label >Last Name</label>
<div >
<input type="text" id="last_name" name="last_name"
placeholder="Enter Last Name" value="" required="">
</div>
</div>
<div >
<label >Email</label>
<div >
<input type="text" id="email" name="email"
placeholder="Enter Email" value="" required="">
</div>
</div>
<div >
<label >Comment Tone</label>
<div >
<select name="comment_tone" id="comment_tone" >
<option value="1">Positive</option>
<option value="0">Negative</option>
</select>
</div>
</div>
<div >
<label >Comment Type</label>
<div >
<select name="comment_type" id="comment_type">
<option value="CO">Conclusion Comments</option>
<option value="RO">Results Comments</option>
</select>
</div>
</div>
<div >
<button type="submit" id="btn-add" value="addNewCommentUser">Save
</button>
<button type="submit" id="btn-save" value="UpdateCommentUser">Save
changes
</button>
</div>
</form>
</div>
<div >
</div>
</div>
</div>
</div>
</x-app-layout>
<!-- end bootstrap model -->
<script>
$(document).ready(function ($) {
fetchCommentUser(); // Get the table from the dB to start
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
function fetchCommentUser() {
$.ajax({
type: "GET",
url: "fetch-comments-user",
dataType: 'json',
success: function (res) {
$('tbody').html("");
$.each(res.comments, function (key, item) {
$('tbody').append('<tr>\
<td><input type="checkbox" name="comments_to_copy" id="comments_to_copy' item.id '"/></td>\
<td>' item.id '</td>\
<td>' item.comment_body '</td>\
<td>' item.first_name '</td>\
<td>' item.last_name '</td>\
<td>' item.email '</td>\
<td>' item.comment_tone '</td>\
<td>' item.comment_type '</td>\
</tr>');
});
},
complete: function () {
isChecked();
}
});
}
$('#addNewCommentUser').click(function (evt) {
evt.preventDefault();
$('#addEditCommentFormUser').trigger("reset");
$('#commentsCrudModel').html("Add Comment");
$('#btn-add').show();
$('#btn-save').hide();
$('#comments-crud-model').modal('show');
});
$('body').on('click', '#btn-add', function (event) {
event.preventDefault();
var comment_body = $("#comment_body").val();
var first_name = $("#first_name").val();
var last_name = $("#last_name").val();
var email = $("#email").val();
var comment_tone = $("#comment_tone").val();
var comment_type = $("#comment_type").val();
var verified_status = 0
$("#btn-add").html('Please Wait...');
$("#btn-add").attr("disabled", true);
$.ajax({
type: "POST",
url: "save-comment-user",
data: {
comment_body: comment_body,
first_name: first_name,
last_name: last_name,
email: email,
comment_tone: comment_tone,
comment_type: comment_type,
verified_status: verified_status,
},
dataType: 'json',
success: function (res) {
console.log(res);
if (res.status == 400) {
$('#msgList').html("");
$('#msgList').addClass('alert alert-danger');
$.each(res.errors, function (key, err_value) {
$('#msgList').append('<li>' err_value '</li>');
});
$('#btn-save').text('Save changes');
} else {
$('#message').html("");
$('#message').addClass('alert alert-success');
$('#message').text(res.message);
fetchCommentUser();
}
},
complete: function () {
$("#btn-add").html('Save');
$("#btn-add").attr("disabled", false);
$("#btn-add").hide();
$('#comments-crud-model').modal('hide');
$('#message').fadeOut(4000);
}
});
});
$('body').on('click', '.edit', function (evt) {
evt.preventDefault();
var id = $(this).data('id');
$.ajax({
type: "GET",
url: "edit-comment-user/" id,
dataType: 'json',
success: function (res) {
console.dir(res);
$('#commentsCrudModel').html("Edit Comment");
$('#btn-add').hide();
$('#btn-save').show();
$('#comments-crud-model').modal('show');
if (res.status == 404) {
$('#msgList').html("");
$('#msgList').addClass('alert alert-danger');
$('#msgList').text(res.message);
} else {
$('#comment_body').val(res.comment.comment_body);
$('#first_name').val(res.comment.first_name);
$('#last_name').val(res.comment.last_name);
$('#email').val(res.comment.email);
$('#comment_tone').val(res.comment.comment_tone);
$('#comment_type').val(res.comment.comment_type);
$('#verified_status').val(res.comment.verified_status);
$('#id').val(res.comment.id);
}
}
});
});
$('body').on('click', '.delete', function (evt) {
evt.preventDefault();
if (confirm("Delete Comment?") == true) {
var id = $(this).data('id');
$.ajax({
type: "DELETE",
url: "delete-comment-user/" id,
dataType: 'json',
success: function (res) {
if (res.status == 404) {
$('#message').addClass('alert alert-danger');
$('#message').text(res.message);
} else {
$('#message').html("");
$('#message').addClass('alert alert-success');
$('#message').text(res.message);
}
fetchCommentUser();
}
});
}
});
$('body').on('click', '#btn-save', function (event) {
event.preventDefault();
var id = $("#id").val();
var comment_body = $("#comment_body").val();
var first_name = $("#first_name").val();
var last_name = $("#last_name").val();
var email = $("#email").val();
var comment_tone = $("#comment_tone").val();
var comment_type = $("#comment_type").val();
var verified_status = $("#verified_status").val();
$("#btn-save").html('Please Wait...');
$("#btn-save").attr("disabled", true);
$.ajax({
type: "PUT",
url: "update-comment-user/" id,
data: {
comment_body: comment_body,
first_name: first_name,
last_name: last_name,
email: email,
comment_tone: comment_tone,
comment_type: comment_type,
verified_status: verified_status,
},
dataType: 'json',
success: function (res) {
console.log(res);
if (res.status == 400) {
$('#msgList').html("");
$('#msgList').addClass('alert alert-danger');
$.each(res.errors, function (key, err_value) {
$('#msgList').append('<li>' err_value '</li>');
});
$('#btn-save').text('Save changes');
} else {
$('#message').html("");
$('#message').addClass('alert alert-success');
$('#message').text(res.message);
fetchCommentUser();
}
},
complete: function () {
$("#btn-save").html('Save changes');
$("#btn-save").attr("disabled", false);
$('#comments-crud-model').modal('hide');
$('#message').fadeOut(4000);
}
});
});
$("#btnGet").click(function () {
var message = "";
// Loop through all checked CheckBoxes in GridView.
$("#Table1 input[type=checkbox]:checked").each(function () {
var row = $(this).closest("tr")[0];
message = " " row.cells[2].innerHTML;
message = "\n-----------------------\n";
});
// Display selected Row data in Alert Box.
$("#messageList").html(message);
return false;
});
$("#copy").click(function () {
$("#messageList").select();
document.execCommand("copy");
alert("Copied On clipboard");
});
function isChecked() {
$("#Table1 input[type=checkbox]").each(function () {
if ($(this).val() == 1) {
$(this).prop("checked", true);
} else {
$(this).prop("checked", false);
}
});
}
});
</script>
</body>
</html>
CodePudding user response:
Try this where condition in the fetchCommentUser method:
public function fetchCommentUser()
{
$comments = Comment::where('verified_status', 1)->get();
return response()->json([
'comments' => $comments,
]);
}
With that user will get ONLY verified comments.