Coder Social home page Coder Social logo

commentable's Introduction

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:

  1. Inherit from the Commentable and Authorable models, provided by Commentable. This way, you also get acces to the helper methods provided by these models.

  2. Implement the CommentableInterface and AuthorableInterface interfaces in your models. This is useful, if you don't inherit from Eloquent in your models (maybe you use Ardent or something).

  3. 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 CommentInterfaceinterface 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 by php artisan migrate --package=petersuhm/commentable.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.