D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
home
/
forge
/
re-viewers.com
/
app
/
Http
/
Controllers
/
Admin
/
Filename :
CommentController.php
back
Copy
<?php namespace App\Http\Controllers\Admin; use App\Http\Controllers\Controller; use App\Models\Comment; // Assuming you have a Comment model use App\Models\Review; // Assuming you have a Review model use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; class CommentController extends Controller { public function index() { $page_title = "Manage Comments"; // This query fetches reviews that have comments and counts the comments for each $reviews = Review::withCount('comments') ->has('comments') ->orderByDesc('created_at') ->get(); // You'll need to fetch the last comment date as well foreach ($reviews as $review) { $lastComment = Comment::where('product_id', $review->id) ->latest() ->first(); $review->last_comment_date = $lastComment ? $lastComment->created_at->timestamp : null; } return view('pages.comments.index', compact('page_title', 'reviews')); } public function remove(Request $request) { $request->validate([ 'product_id' => 'required', ]); $product_id = $request->input('product_id'); // Delete all comments related to this product Comment::where('product_id', $product_id)->delete(); return response()->json(['status' => 1, 'msg' => 'Comments removed successfully.']); } public function removeAll(Request $request) { $request->validate([ 'ids' => 'required|array', ]); $ids = $request->input('ids'); // Delete all comments for the selected products Comment::whereIn('product_id', $ids)->delete(); return response()->json(['status' => 1, 'msg' => 'All selected comments were removed successfully.']); } public function view($productId) { $review = Review::with('category')->findOrFail($productId); $comments = Comment::where('product_id', $productId) ->orderBy('created_at', 'DESC') ->get(); $groupedComments = $comments->groupBy(function ($comment) { return $comment->created_at->format('d M Y'); }); $totalComments = $comments->count(); // Pass data to the view return view('pages.comments.view', compact('review', 'groupedComments', 'totalComments')); } // You also need a method to handle a single comment deletion public function removeSingleComment(Request $request) { $request->validate([ 'id' => 'required|integer', ]); $comment = Comment::findOrFail($request->id); $comment->delete(); return response()->json([ 'status' => '1', 'msg' => 'Comment deleted successfully.' ]); } }