alibayat / laravel-commentable
为Laravel的Eloquent模型实现评论系统。
dev-master / 1.1.x-dev
2024-05-29 05:39 UTC
Requires
- php: ^7.2|^8.0|^8.1|^8.2|^8.3
- illuminate/database: ^7.0|^8.0|^9.0|^10.0|^11.0
- illuminate/support: ^7.0|^8.0|^9.0|^10.0|^11.0
- kalnoy/nestedset: ^6.0
This package is auto-updated.
Last update: 2024-09-29 06:20:14 UTC
README
本包简化了为Eloquent模型实现评论系统的过程。只需在模型中使用该特性即可。
要求
- PHP 7.2+
- Laravel 7+
安装
composer require alibayat/laravel-commentable
发布并运行迁移
php artisan vendor:publish --provider="AliBayat\LaravelCommentable\CommentableServiceProvider"
php artisan migrate
Laravel Commentable 包将由Laravel自动发现。如果没有:请手动在config/app.php的providers数组中注册该包。
'providers' => [ ... \AliBayat\LaravelCommentable\CommentableServiceProvider::class, ],
设置模型 - 只需在模型中使用特性。
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use AliBayat\LaravelCommentable\Commentable; class Post extends Model { use Commentable; }
用法
use App\Models\User; use App\Models\Post; use AliBayat\LaravelCommentable\Comment; // assuming that we have these variables $user = User::first(); $post = Post::first();
为文章创建评论
$commentData = [ 'title' => 'comment title (nullable)', 'body' => 'comment body' ]; $post->comment($commentData, $user);
为文章创建子评论
$parentComment = Comment::first(); $childCommentData = [ 'title' => 'comment title (nullable)', 'body' => 'comment body' ]; $post->comment($childCommentData, $user, $parentComment);
更新文章的评论
$comment = Comment::first(); $newData = [ 'body' => 'new body of the comment to update' ]; $post->updateComment($comment->id, $newData);
删除单个文章评论
$comment = Comment::first(); $post->deleteComment($comment->id);
删除文章的所有评论
$post->comments()->delete();
检查评论是否有子评论(布尔值)
$comment = Comment::first(); $comment->hasChildren();
统计文章的评论数量
$post->commentCount();
显示文章的评论
$post->allComments(); // shows all comments (including children) $post->comments(); // shows only top level comments
激活
默认情况下,当您创建评论时,它将被存储为非激活评论,除非您提供'active'字段并将其设置为true。
$activeComment = [ 'body' => 'comment body', 'active' => true ]; $comment = $post->comment($activeComment, $user);
但您始终可以通过以下方法更改评论的激活状态:
激活
$comment->active(); // returns a boolean indicating the state of operation
停用
$comment->deactivate(); // returns a boolean indicating the state of operation
关系
评论关系
$postWithComments = Post::with('comments')->get(); // returns a collection of all comments associated with the post
活动评论关系
$postWithActiveComments = Post::with('activeComments')->get(); // returns a collection of all active comments associated with the post
父关系
$comment = Comments::latest()->first(); $comment->parent; // returns the comment's parent if available
子关系
$comment = Comments::latest()->first(); $comment->children; // returns the comment's children if available
祖先关系
$comment = Comments::latest()->first(); $comment->ancestors; // return the comment's ancestors if available
后代关系
$comment = Comments::latest()->first(); $comment->descendants; // return the comment's descendants if available
附加功能
感谢伟大的laravel-nestedset 包,您有权访问一些附加功能,我们在此处回顾了一些,但您始终可以参考包的存储库以获取完整的文档。
toTree()
$post->comments->toTree(); // returns a collection of the comment's tree structure associated with the post
toFlatTree()
$post->comments->toFlatTree(); // return a collection of the comment's flat tree structure associated with the post
saveAsRoot()
$comment = $post->comments()->latest()->first(); $comment->saveAsRoot(); // Implicitly change the comment's position to Root // returns boolean
makeRoot()
$comment = $post->comments()->latest()->first(); $comment->makeRoot()->save(); // Explicitly change the comment's position to Root // returns boolean