lakm/laravel-comments-admin-panel
Installation
composer require lakm/laravel-comments-admin-panel
php artisan vendor:publish --provider="Lakm\CommentsAdminPanel\CommentsAdminPanelServiceProvider"
php artisan migrate
config/comments-admin-panel.php and resources/views/vendor/comments-admin-panel/.First Use Case
routes/web.php:
Route::middleware(['auth:sanctum', 'admin'])->group(function () {
Route::resource('comments', \Lakm\CommentsAdminPanel\Http\Controllers\CommentController::class);
});
admin middleware is defined (e.g., via Gate::before() or a custom middleware).Model Discovery
config/comments-admin-panel.php to specify which models support comments:
'models' => [
\App\Models\Post::class,
\App\Models\Article::class,
],
php artisan comments-admin-panel:discover to auto-detect commentable models (if using traits).CRUD Operations
CommentController with methods for:
index): Supports pagination, filtering by model, and status.show): Includes related commentable model data.edit/update): Soft-deletes, replies, and moderation actions.destroy): Soft-deletes by default (configurable).// Filter comments by model and status
$comments = $this->commentRepository->getComments([
'commentable_type' => \App\Models\Post::class,
'status' => 'approved',
]);
Model Integration
Commentable trait or manually define relationships:
use Lakm\Comments\Traits\Commentable;
class Post extends Model
{
use Commentable;
// ...
}
Comment model or use the CommentController to add custom logic:
// Override the controller to add custom fields
public function update(Request $request, Comment $comment) {
$validated = $request->validate([
'content' => 'required',
'custom_field' => 'nullable|string',
]);
$comment->update($validated);
}
Authorization
Gate::define('manage-comments', function (User $user) {
return $user->isAdmin();
});
Route::middleware(['auth:sanctum', 'can:manage-comments'])->group(...);
API Integration
API_ENABLED=true in config). Example:
Route::apiResource('comments', \Lakm\CommentsAdminPanel\Http\Controllers\Api\CommentController::class);
Dynamic Model Binding
Route::get('/comments/{commentable}', [CommentController::class, 'index'])
->name('comments.index');
commentable parameter in the controller:
public function index(Commentable $commentable) {
return $this->commentRepository->getCommentsFor($commentable);
}
Bulk Actions
CommentController to support bulk approval/rejection:
public function bulkUpdate(Request $request) {
$request->validate(['status' => 'required|in:approved,rejected']);
$this->commentRepository->updateStatus($request->ids, $request->status);
}
Event Listeners
CommentCreated, CommentApproved) to trigger notifications:
Comment::created(function (Comment $comment) {
Notification::route('mail', $comment->user->email)
->notify(new CommentCreatedNotification($comment));
});
Custom Views
php artisan vendor:publish --tag=comments-admin-panel-views
resources/views/vendor/comments-admin-panel/comments/index.blade.php).Model Discovery Issues
php artisan comments-admin-panel:discover fails to detect models.Commentable trait or manually define commentable() in config/comments-admin-panel.php:
'models' => [
\App\Models\Post::class => [
'commentable' => true,
],
],
Soft Deletes Conflicts
php artisan comments-admin-panel:prune to clean up orphaned records or ensure deleted_at is nullable in the comments table.Authorization Bypass
routes/web.php and gates/policies:
// Ensure this runs before the route group
Gate::before(function (User $user) {
if ($user->isAdmin()) {
return true;
}
});
Performance with Large Datasets
commentable_type and commentable_id in the comments table:
Schema::table('comments', function (Blueprint $table) {
$table->index(['commentable_type', 'commentable_id']);
});
$comments = Comment::with('commentable')->get();
Log Query Builder SQL
AppServiceProvider:
public function boot() {
if (config('app.debug')) {
DB::enableQueryLog();
}
}
$comments = $this->commentRepository->getComments([]);
\Log::debug(DB::getQueryLog());
Check Published Config
config/comments-admin-panel.php after publishing:
php artisan config:clear
Validate Middleware
Route::get('/test-auth', function () {
return "Authorized";
})->middleware(['auth:sanctum', 'admin']);
Custom Repository
namespace App\Repositories;
use Lakm\CommentsAdminPanel\Repositories\CommentRepository as BaseRepository;
class CommentRepository extends BaseRepository {
public function getSpamComments() {
return $this->model->where('status', 'pending')
->where('content', 'like', '%spam%')
->get();
}
}
AppServiceProvider:
$this->app->bind(
\Lakm\CommentsAdminPanel\Repositories\CommentRepository::class,
App\Repositories\CommentRepository::class
);
Custom Actions
@foreach($comments as $comment)
<td>
<form action="{{ route('comments.bulk') }}" method="POST">
@csrf
<input type="hidden" name="ids[]" value="{{ $comment->id }}">
<button type="submit" class="btn btn-sm btn-warning">
Flag as Spam
</button>
</form>
</td>
@endforeach
Webhook Integration
Comment::created(function (Comment $comment) {
Http::post('https://api.example.com/webhooks/comments', [
'comment_id' => $comment->id,
]);
});
Localization
php artisan vendor:publish --tag=comments-admin-panel-lang
resources/lang/en/comments-admin-panel.php.How can I help you explore Laravel packages today?