• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

beyondcode/laravel-comments: Add comments to your Laravel application

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

beyondcode/laravel-comments

开源软件地址(OpenSource Url):

https://github.com/beyondcode/laravel-comments

开源编程语言(OpenSource Language):

PHP 100.0%

开源软件介绍(OpenSource Introduction):

Add comments to your Laravel application

Latest Version on Packagist Build Status Quality Score Total Downloads

Add the ability to associate comments to your Laravel Eloquent models. The comments can be approved and nested.

$post = Post::find(1);

$post->comment('This is a comment');

$post->commentAsUser($user, 'This is a comment from someone else');

Installation

You can install the package via composer:

composer require beyondcode/laravel-comments

The package will automatically register itself.

You can publish the migration with:

php artisan vendor:publish --provider="BeyondCode\Comments\CommentsServiceProvider" --tag="migrations"

After the migration has been published you can create the media-table by running the migrations:

php artisan migrate

You can publish the config-file with:

php artisan vendor:publish --provider="BeyondCode\Comments\CommentsServiceProvider" --tag="config"

Usage

Registering Models

To let your models be able to receive comments, add the HasComments trait to the model classes.

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use BeyondCode\Comments\Traits\HasComments;

class Post extends Model
{
    use HasComments;
    ...
}

Creating Comments

To create a comment on your commentable models, you can use the comment method. It receives the string of the comment that you want to store.

$post = Post::find(1);

$comment = $post->comment('This is a comment from a user.');

The comment method returns the newly created comment class.

Sometimes you also might want to create comments on behalf of other users. You can do this using the commentAsUser method and pass in your user model that should get associated with this comment:

$post = Post::find(1);

$comment = $post->commentAsUser($yourUser, 'This is a comment from someone else.');

Approving Comments

By default, all comments that you create are not approved - this is just a boolean flag called is_approved that you can use in your views/controllers to filter out comments that you might not yet want to display.

To approve a single comment, you may use the approve method on the Comment model like this:

$post = Post::find(1);
$comment = $post->comments->first();

$comment->approve();

Auto Approve Comments

If you want to automatically approve a comment for a specific user (and optionally model) you can let your User model implement the following interface and method:

namespace App\Models;

use BeyondCode\Comments\Contracts\Commentator;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable implements Commentator
{
    /**
     * Check if a comment for a specific model needs to be approved.
     * @param mixed $model
     * @return bool
     */
    public function needsCommentApproval($model): bool
    {
        return false;    
    }
    
}

The needsCommentApproval method received the model instance that you want to add a comment to and you can either return true to mark the comment as not approved, or return false to mark the comment as approved.

Retrieving Comments

The models that use the HasComments trait have access to it's comments using the comments relation:

$post = Post::find(1);

// Retrieve all comments
$comments = $post->comments;

// Retrieve only approved comments
$approved = $post->comments()->approved()->get();

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap