PHPackages                             fbf/laravel-comments - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. fbf/laravel-comments

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

fbf/laravel-comments
====================

A Laravel 4 package for adding commenting to a website that has user accounts

v0.3.1(11y ago)204.6k6[2 issues](https://github.com/FbF/Laravel-Comments/issues)MITPHPPHP &gt;=5.3.0

Since Mar 31Pushed 11y ago3 watchersCompare

[ Source](https://github.com/FbF/Laravel-Comments)[ Packagist](https://packagist.org/packages/fbf/laravel-comments)[ RSS](/packages/fbf-laravel-comments/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (3)Dependencies (1)Versions (5)Used By (0)

Laravel Comments
================

[](#laravel-comments)

A Laravel 4 package for adding commenting to a website that has user accounts

Features
--------

[](#features)

- Attach comments to any/all existing models in your app. Comment model is polymorphic.
- Comments belong to a user (there is no anonymous commenting functionality built in at the moment)
- Includes a migration, model, controller for adding comments, sample partials for showing the comments list and the add comment form
- After adding a comment, user is redirected back to previous page, anchored to their new comment.
- A FrozenNode/Laravel-Administrator config file for administrators to view, edit and delete submitted comments

Installation
------------

[](#installation)

Add the following to you composer.json file (Recommend swapping "dev-master" for the latest release)

```
"fbf/laravel-comments": "dev-master"

```

Run

```
composer update

```

Add the following to app/config/app.php

```
'Fbf\LaravelComments\LaravelCommentsServiceProvider'

```

Run the package migration

```
php artisan migrate --package=fbf/laravel-comments

```

Publish the config

```
php artisan config:publish fbf/laravel-comments

```

Add your namespaced models to the list of commentable models in the `config/commentables.php` config file (this is just for validation purposes)

Optionally tweak the settings in the other config files for your app.

Optionally copy the administrator config file (`src/config/administrator/posts.php`) to your administrator model config directory.

Configuration
-------------

[](#configuration)

See the configuration options in the files in the config directory

Administrator
-------------

[](#administrator)

You can use the excellent Laravel Administrator package by FrozenNode to administer your comments.

A ready-to-use model config file for the Comment model (commentsphp) is provided in the src/config/administrator directory of the package, which you can copy into the app/config/administrator directory (or whatever you set as the model\_config\_path in the administrator config file).

Usage
-----

[](#usage)

Add the polymorphic / hasMany relationship to the models in your app that you want to be commentable:

```
/**
 * Defines the polymophic hasMany / morphMany relationship between Post and Comment
 *
 * @return mixed
 */
public function comments()
{
    return $this->morphMany('Fbf\LaravelComments\Comment', 'commentable');
}
```

Add the comments partial to the blade templates where you want the comments displayed, passing in the instance of the commentable model, and the comments.

```
@include('laravel-comments::comments', array('commentable' => $post, 'comments' => $post->comments))
```

The above is an example for adding comments to a Post model. If you want comments on something else, obviously change the mentions of the word 'Post' or '$post' to that of your model.

To use in conjunction with fbf/laravel-blog, (or the model in any other package for that matter), see the section in the readme about extending the Post model in your own app. I.e. you need to extend the Post model in your app/models directory and add the relationship in that file, rather than adding this to the model file in the vendor directory.

To note...
----------

[](#to-note)

To make integration really easy, and to increase performance, there's a line in the `list.blade.php` view partial that lazy eager loads the user data for each comment. If you override this view partial, and you omit this line, when you try to show the username of the user who wrote each comment, you may cause a big performance hit on your database, as it makes a separate query for each comment's author:

```
{{-- Lazy eager load the user data for each comment, this is for --}}
{{-- performance reasons to mitigate against the n+1 query problem --}}
