Awesome
Commentable
Commentable is a comments model for Laravel 4.
Installation
Require "petersuhm/commentable": "dev-master"
in your composer.json
file and
run composer update
:
"require-dev": {
"petersuhm/commentable": "dev-master"
}
Add the CommentableServiceProvider
to the providers
array in
app/config/app.php
:
'providers' => array(
...
'Petersuhm\Commentable\CommentableServiceProvider',
);
Add a Comment
alias to the aliases
array in app/config/app.php
:
'aliases' => array(
...
'Comment' => 'Petersuhm\Commentable\Comment',
);
Finally, you need to run the migration for the petersuhm_commentable_comments
table:
php artisan migrate --package=petersuhm/commentable
Basic usage
In order to use Commentable's Comment
model, you have the following
options:
-
Inherit from the
Commentable
andAuthorable
models, provided by Commentable. This way, you also get acces to the helper methods provided by these models. -
Implement the
CommentableInterface
andAuthorableInterface
interfaces in your models. This is useful, if you don't inherit from Eloquent in your models (maybe you use Ardent or something). -
A mixture of both.
Inheritance
If you choose to let your models inherit from the Commentable
and Authorable
models, provided by Commentable, you gain access the helper methods, that these
classes offer. This is the easiest way to utilise Comment in your app. You
simply declare your models this way:
use Petersuhm\Commentable\Authorable;
use Petersuhm\Commentable\Commentable;
class User extends Authorable {}
class BlogPost extends Commentable {}
Interfaces
If you don't wish to inherit from the Commentable
and Authorable
models, you
can instead implement the CommentableInterface
and AuthorableInterface
interfaces. This way, you will not be able to use the helper methods from
Commentable
and Authorable
, unless you add them to your own models. In order
to use Commentable's interfaces, declare your models this way:
use Petersuhm\Commentable\AuthorableInterface;
use Petersuhm\Commentable\CommentableCommentable;
class User extends Eloquent implements AuthorableInterface {
public function comments()
{
return $this->morphMany('Comment', 'authorable');
}
}
class BlogPost extends Eloquent implements CommentableInterface {
public function comments()
{
return $this->morphMany('Comment', 'commentable');
}
}
Adding comments
The Comment
class has an add()
function, which returns an unsaved comment
instance. You can add a new comment like this:
$body = 'This is a test comment';
$authorable = User::first();
$commentable = BlogPost::first();
Comment::add($body, $authorable, $commentable)->save();
If you use inheritance, you can also use the addComment
method:
$body = 'This is a test comment';
$authorable = User::first();
$commentable = BlogPost::first();
$commentable->addComment($body, $authorable)->save();
// Or the other way around:
$authorable->addComment($body, $commentable)->save();
Retrieving comments
Thanks to Eloquent, you can retrieve comments just like you would expect:
$authorable = User::first();
$commentable = BlogPost::first();
$authorable->comments;
$commentable->comments;
Advanced usage
If you wish to overwrite the Comment
class provided by Commentable, you can do
so by implementing the CommentInterface
interface in your own Comment
model.
Support and suggestions
If you have any problems, or if you have some suggestions, discover a bug etc., feel free to open an issue!
Changelog
- Added indexes to comments table: Run
composer update
followed byphp artisan migrate --package=petersuhm/commentable
.