Installation
composer require atm/commentbundle
Add to config/bundles.php:
return [
// ...
Atm\CommentBundle\AtmCommentBundle::class => ['all' => true],
];
Publish Assets
php artisan atm:comment:install
This creates:
database/migrations/..._create_comments_table.php)config/atm_comment.php)resources/views/vendor/atm_comment/)Run Migrations
php artisan migrate
First Use Case Add a commentable route in your controller:
use Atm\CommentBundle\Comment\CommentableInterface;
class ArticleController extends Controller implements CommentableInterface
{
public function show($id)
{
$article = Article::findOrFail($id);
return view('articles.show', [
'article' => $article,
'comments' => $article->comments, // Automatically loaded via trait
]);
}
}
Enable Comments in Blade
@include('atm_comment::partials/comment_form', ['commentable' => $article])
@include('atm_comment::partials/comment_list', ['commentable' => $article])
Extend Atm\CommentBundle\Comment\CommentableTrait in your Eloquent models:
use Atm\CommentBundle\Comment\CommentableTrait;
class Article extends Model
{
use CommentableTrait;
// ...
}
comments() relationship.$commentableType and $commentableId for polymorphic comments.Override the default form by publishing views:
php artisan vendor:publish --tag=atm_comment.views
Modify resources/views/vendor/atm_comment/partials/comment_form.blade.php.
Extend validation in config/atm_comment.php:
'validation' => [
'rules' => [
'content' => 'required|string|min:10',
'parent_id' => 'nullable|exists:comments,id',
],
'messages' => [
'content.required' => 'Your comment cannot be empty!',
],
],
Enable threading in config:
'threading' => [
'enabled' => true,
'max_depth' => 5,
],
Use reply() method in Blade:
@include('atm_comment::partials/comment_reply_form', ['comment' => $comment])
Use the CommentController or create a custom API endpoint:
use Atm\CommentBundle\Http\Controllers\CommentController as BaseCommentController;
class ApiCommentController extends BaseCommentController
{
public function __construct()
{
$this->middleware('auth:api');
}
}
Listen to comment events (e.g., CommentCreated):
use Atm\CommentBundle\Events\CommentCreated;
class NotifyModerator
{
public function handle(CommentCreated $event)
{
// Send notification to moderators
}
}
Register in EventServiceProvider:
protected $listen = [
CommentCreated::class => [
NotifyModerator::class,
],
];
Enable soft deletes in config:
'soft_deletes' => true,
Use restore() and forceDelete() on comments.
Add search functionality:
use Atm\CommentBundle\Scout\CommentScout;
class Article extends Model
{
use CommentableTrait, CommentScout;
// ...
}
Trigger notifications on new comments:
use Atm\CommentBundle\Notifications\NewCommentNotification;
// In your controller
event(new CommentCreated($comment));
$comment->commentable->notify(new NewCommentNotification($comment));
Fetch comments via API:
// Vue example
this.comments = await axios.get(`/api/commentables/${this.article.id}/comments`);
Use Atm\CommentBundle\Http\Controllers\AdminCommentController for moderation:
Route::resource('admin/comments', 'Atm\CommentBundle\Http\Controllers\AdminCommentController');
Polymorphic Relationships
$commentableType and $commentableId manually when not using the trait.Atm\CommentBundle\Comment\Comment::create([...]) with explicit type/ID:
Comment::create([
'content' => 'Test',
'commentable_type' => Article::class,
'commentable_id' => $article->id,
]);
Caching Comments
with() in eager loading).fresh():
$article = Article::with('comments')->find($id);
$article->comments()->fresh(); // Force reload
Threading Depth
max_depth is too high.3-5) in config.Permission Denied
403 errors when submitting comments without proper middleware.Route::post('/comments', 'CommentController@store')->middleware('auth');
Database Locking
DB::connection()->disableQueryLog();
// Comment operations
DB::connection()->enableQueryLog();
CSRF Token Mismatch
@csrf
Or for AJAX:
headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') }
Query Logging Enable Laravel debug mode:
APP_DEBUG=true
Check queries in storage/logs/laravel.log.
Commentable Model Check
Verify the model implements CommentableInterface:
class Article implements CommentableInterface { ... }
Middleware Order
Ensure Atm\CommentBundle\Http\Middleware\VerifyCommentable is loaded before auth middleware.
Event Debugging
Listen for events in tinker:
php artisan tinker
>>> event(new \Atm\CommentBundle\Events\CommentCreated($comment));
View Overrides Clear cached views after publishing:
php artisan view:clear
Custom Comment Models
Extend Atm\CommentBundle\Entity\Comment:
class CustomComment extends Comment
{
protected $table = 'custom_comments';
}
Update config:
'model' => Atm\CommentBundle\Entity\CustomComment::class,
Custom Storage Override storage engine (e.g., Redis):
use Atm\CommentBundle\Comment\CommentRepositoryInterface;
class RedisCommentRepository implements CommentRepositoryInterface
{
// Implement storage logic
}
Bind in ServiceProvider:
$this->app->bind(
CommentRepositoryInterface::class,
RedisCommentRepository::class
);
Custom Validation Create a custom validator:
use Atm\CommentBundle\Validation\CommentValidatorInterface;
class CustomCommentValidator implements CommentValidatorInterface
{
public function validate(array $data, $commentable)
{
// Custom logic
return $data;
}
}
Bind in config:
'validator' => Atm\CommentBundle\Validation\CustomCommentValidator::class,
**Custom Not
How can I help you explore Laravel packages today?