baklysystems/laravel-commentable

允许将带级联的评论添加到多个和不同的模型中。

v2.0 2019-09-08 13:34 UTC

This package is auto-updated.

Last update: 2024-09-09 00:58:34 UTC


README

允许在Laravel 4和5的应用中为多个和不同的模型添加带级联的评论。

对于Laravel 4,请查看1.0分支

此包使用嵌套集模式与Baum

更多关于嵌套集的信息

安装

编辑您的项目composer.json文件,以要求lanz/laravel-commentable

"require": {
  "lanz/laravel-commentable": "~2.0"
}

接下来,从终端更新Composer。

composer update

与大多数Laravel包一样,您需要注册Commentable 服务提供者。在您的config/app.php中,将'Lanz\Commentable\CommentableServiceProvider'添加到$providers数组末尾。

'providers' => [

    'Illuminate\Foundation\Providers\ArtisanServiceProvider',
    'Illuminate\Auth\AuthServiceProvider',
    ...
    'Lanz\Commentable\CommentableServiceProvider',

],

入门

在正确安装包之后,您需要生成迁移。

php artisan commentable:migration

它将生成<timestamp>_create_comments_table.php迁移。现在,您可以使用artisan migrate命令运行它

php artisan migrate

迁移后,将出现一个新表,comments

用法

您需要在您的模型上设置它作为可评论的。

<?php namespace App;

use Lanz\Commentable\Commentable;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{

    use Commentable;

}

现在,您的模型可以访问comments方法。

$post = Post::first();

$comment = new Lanz\Commentable\Comment;
$comment->body = 'My first comment!';
$comment->user_id = \Auth::id();

$post->comments()->save($comment);

dd(Post::first()->comments);

有关带级联评论的所有信息,请参阅Baum文档