Laravel新手求助:如何使用actuallymab/laravel-comment评论系统?
Hey there! Let's get that comment system working smoothly—you've already nailed the first two big steps (Composer install and migrations), so we're halfway there. Let's fill in the gaps and get this up and running.
1. Finish Setting Up Your Models
First, I assume you started adding the trait to your Post model but didn't finish it—here's what you need:
In your Post.php model, add the CanBeCommented trait, just like you added CanComment to your User model:
use Actuallymab\LaravelComment\CanBeCommented; class Post extends Model { use CanBeCommented; // ... rest of your model code }
That trait automatically sets up the comments relationship for your Post, so you can later fetch all comments with $post->comments.
2. Create a Controller to Handle Comment Submissions
Let's make a simple CommentController to handle creating comments. Run this Artisan command first:
php artisan make:controller CommentController
Then add the store method to handle form submissions:
namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Post; class CommentController extends Controller { public function store(Request $request, Post $post) { // Validate the comment content $request->validate([ 'content' => 'required|string|max:500' ]); // Use the comment() method from the CanComment trait on User auth()->user()->comment($post, $request->content); // Redirect back to the post page with a success message return back()->with('success', 'Comment added successfully!'); } }
3. Add Routes for Comments
Open up your routes/web.php file and add this route to handle comment submissions:
use App\Http\Controllers\CommentController; Route::post('/posts/{post}/comments', [CommentController::class, 'store'])->name('comments.store');
4. Add a Comment Form and Display Existing Comments
Now let's add the frontend part. On your post show page (like resources/views/posts/show.blade.php), add this form to let users submit comments:
@auth <form action="{{ route('comments.store', $post) }}" method="POST"> @csrf <div class="mb-3"> <textarea name="content" class="form-control" rows="3" placeholder="Add your comment..." required></textarea> </div> <button type="submit" class="btn btn-primary">Post Comment</button> </form> @endauth @guest <p>Please <a href="{{ route('login') }}">log in</a> to leave a comment.</p> @endguest
Then add this section to display all existing comments for the post:
<div class="mt-4"> <h4>Comments ({{ $post->comments->count() }})</h4> @foreach($post->comments as $comment) <div class="card mb-2"> <div class="card-body"> <p class="card-text">{{ $comment->content }}</p> <p class="card-text small text-muted"> By {{ $comment->commenter->name }} on {{ $comment->created_at->format('M d, Y H:i') }} </p> </div> </div> @endforeach @if($post->comments->isEmpty()) <p>No comments yet—be the first to comment!</p> @endif </div>
5. Optional: Add Comment Replies (If You Need Them)
If you want to let users reply to comments, the package supports this out of the box. Here's a quick snippet to add reply functionality:
In your comment loop, add a reply form (only for authenticated users):
@auth <button class="btn btn-sm btn-outline-secondary" type="button" data-bs-toggle="collapse" data-bs-target="#replyForm-{{ $comment->id }}"> Reply </button> <div class="collapse mt-2" id="replyForm-{{ $comment->id }}"> <form action="{{ route('comments.store', $post) }}" method="POST"> @csrf <input type="hidden" name="parent_id" value="{{ $comment->id }}"> <textarea name="content" class="form-control form-control-sm" rows="2" placeholder="Add your reply..." required></textarea> <button type="submit" class="btn btn-sm btn-primary mt-1">Post Reply</button> </form> </div> @endauth <!-- Display replies --> @if($comment->children->isNotEmpty()) <div class="ms-4 mt-2"> @foreach($comment->children as $reply) <div class="card mb-1"> <div class="card-body p-2"> <p class="card-text small">{{ $reply->content }}</p> <p class="card-text text-muted smaller"> By {{ $reply->commenter->name }} on {{ $reply->created_at->format('M d, Y H:i') }} </p> </div> </div> @endforeach </div> @endif
And update your store method in CommentController to handle the parent ID:
public function store(Request $request, Post $post) { $request->validate([ 'content' => 'required|string|max:500', 'parent_id' => 'nullable|exists:comments,id' ]); auth()->user()->comment($post, $request->content, $request->parent_id); return back()->with('success', 'Comment added successfully!'); }
That's it! You should now have a fully functional comment system. If you run into any issues (like permissions or validation errors), double-check that your User model is correctly using the CanComment trait and that your routes are pointing to the right controller method.
内容的提问来源于stack exchange,提问作者someguy

