lanz / laravel-commentable
v2.0.5
2015-03-23 10:53 UTC
Requires
- php: >=5.4
- baum/baum: ~1.1
- illuminate/database: ~5.0
- illuminate/support: ~5.0
Requires (Dev)
- phpunit/phpunit: ~4.0
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 文档。