avihs / laravel-post-comment-reply
这将创建一个帖子、评论、点赞和回复,并保存在数据库中
1.0.3
2020-09-05 17:02 UTC
Requires
- php: ^7.2.5
This package is auto-updated.
Last update: 2024-09-14 00:42:19 UTC
README
这将创建一个帖子、评论和回复,并保存在数据库中。
⚠️ 此插件开发用于向下兼容到Laravel 5.6+
安装
使用composer require或添加到composer.json中。
composer require avihs/laravel-post-comment-reply
如果您使用SQL数据库服务器存储日志事件,您需要首先运行迁移。MongoDB驱动不需要迁移。
php artisan migrate
使用方法
由于这是一个自定义包,并且您的模型应该有用户和数据库用户表。
基本用法
首先,将 use Avihs\PostReply\Traits\HasPost trait 添加到您的用户模型中
use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Foundation\Auth\User as Authenticatable; use Avihs\PostReply\Traits\HasPost; class User extends Authenticatable { use HasPost; // ... }
用户
// Fetch the User $user = User::find(1); // get User Posts $posts = $user->posts; // get latest Posts $latestPosts = $user->latest_posts; // get Active Posts $activePosts = $user->active_posts; // get Active Posts $inactivePosts = $user->inactive_posts; // Post Count $postsCount = $user->posts_count; // Active Post Count $activePostsCount = $user->active_posts_count; // Inactive Post Count $inactivePostsCount = $user->inactive_posts_count; /** * Assgin or remove Post * Create or fetch the Post **/ use Avihs\PostReply\Models\Post; //Get the single Post or Post::find(1); $post = Post::create([ 'title'=> 'My First Post', 'description'=> 'This package will create posts, comments, likes, diskies and replies', 'status'=> 1, // default active(1) for inactive(0) ]); //Assign single Post $user->assignPost($post); //Assign multiple Post $user->assignPost([$post, $post2]); //Remove single Post $user->removePost($post); //Remove multiple Post $user->removePost([$post, $post2]); //Synchronize single Post $user->syncPosts($post); //Synchronize multiple Post $user->syncPosts([$post, $post2]);
帖子
use Avihs\PostReply\Models\Post; /** * Get all Posts * @return Array|Posts|Comments|Replies * Response all Posts with their comments and replies on each comment **/ $posts = Post::all(); /** * Fetch a single Post * @return Object|Posts, Array|Comments|Replies * Response Post with all comments and replies on each comment **/ $post = Post::find(1); // Create new Post $post = Post::create([ 'title'=> 'My First Post', 'description'=> 'This package will create posts, comments, likes, diskies and replies', 'status'=> 1, // default active(1) for inactive(0) ]); // Update Post $post->update(['title'=>'new text1']); // Boolean /** * Delete Post * @return Boolean * This will delete the Post as well as all comments & replies of that Post **/ $post->delete(); //Assign a Post to user $post->user()->associate($user)->save(); //Remove a Post $post->user()->dissociate()->save(); // First and Last Post for the User $post = $user->posts->first(); $post = $user->posts->last();
评论
/** * Get all Comments for the Post * @return Array|Comments|Replies * Response all comments and replies for the Post **/ $comments = $post->comments; /** * Add Comment to Post * @return Object|Comment with replies[] **/ $comment = $post->addComment("My First Comment", $user); /** * Add Comment to Post * @return Array|Comments|replies * Response all comments added to the post **/ $comments = $post->addAllComments("This is awesome package", $user); /** * Edit Comment to Post * @return Object|Comment with Array|replies * Response edited comment with replies **/ $comment = $comment->editComment("My Edit Comment" , $OptionalParamsStatus =1); /** * Delete Comment * @return Boolean * This will delete the Comment as well as all replies of that Comment **/ $comment->delete(); // First and Last Comment for the Post $comment = $post->comments->first(); $comment = $post->comments->last(); /** * Fetching a single comment * Get comment details **/ use Avihs\PostReply\Models\Comment; $comment = Comment::find(1); // OR $post->addComment("My First Comment", $user); $commentText = $comment->content; //comment content $commentDate = $comment->created_at; //comment created date $commentUpdate = $comment->updated_at; //comment updated date $commentedByUser = $comment->user; // commented user in Object
回复
/** * Get all Replies for the Comment * @return Array|Replies * Response all Replies for the Comment **/ $replies = $comment->replies; /** * Add Reply to Comment * @return Object|Reply **/ $reply = $comment->addReply("My First Reply", $user); /** * Add Reply to Comment * @return Array|Replies * Response all Replies added to the comment **/ $replies = $comment->addAllReplies("This is awesome package, I like reply feature.", $user); /** * Edit Reply to Comment * @return Object|Reply * Response edited reply **/ $reply = $reply->editReply("My Edit Reply" , $OptionalParamsStatus =1); /** * Delete Reply * @return Boolean * This will delete the Reply **/ $reply->delete(); // First and Last Reply for the Comment $reply = $comment->replies->first(); $reply = $comment->replies->last(); /** * Fetching a single reply * Get reply details **/ use Avihs\PostReply\Models\Reply; $reply = Reply::find(1); // OR $comment->addReply("My First Reply", $user); $replyText = $reply->content; //reply content $replyDate = $reply->created_at; //reply created date $replyUpdate = $reply->updated_at; //reply updated date $repliedByUser = $reply->user; // replied user in Object
Blade
也可以用于laravel blade文件。
@foreach ($posts as $post) <div> <h3>Post Id : {{ $post->id }}</h3> <p>Post title : {{ $post->title }}</p> <p>Post description : {{ $post->description }}</p> <p>Post status : {{ $post->status }}</p> <p>Post created date : {{ $post->created_at }}</p> <p>Post created by User : {{ $post->user->name }}</p> <p>Comments on Post: @foreach ($post->comments as $comment) <ul> <li>Comment : {{ $comment->content }}</li> <li>Comment created date: {{ $comment->created_at }}</li> <li>Comment created by User: {{ $comment->user->name }}</li> <li>Reply on comment : @foreach ($comment->replies as $reply) <ul> <li>Reply : {{ $reply->content }}</li> <li>Reply created date: {{ $reply->created_at }}</li> <li>Reply created by User: {{ $reply->user->name }}</li> </ul> @endforeach </li> </ul> @endforeach </p> </div> @endforeach
高级Eloquent模型
与 & withCount 一起使用
use Avihs\PostReply\Models\Post; $posts = Post::with('user')->where('user_id', 1)->first(); $user = User::with(['posts', 'active_posts', 'inactive_posts', 'latest_posts'])->withCount(['posts', 'active_posts', 'inactive_posts'])->get();
示例
可以使用工厂创建模拟数据,您也可以创建多个模型的Collection或创建给定类型的模型
php artisan tinker factory(Avihs\PostReply\Models\Message::class,50)->create();
单个帖子响应
{ "id":5, "title":"Post Ducimus sint id ut odit vel vel.", "description":"Vel sit iusto repellat aliquid excepturi accusamus aut. Omnis impedit ut sequi rerum ab vitae ea non. Ducimus et vero voluptas nesciunt. Qui dolores praesentium unde tenetur qui.", "status":0, "user_id":1, "created_at":"2020-09-03T13:09:19.000000Z", "updated_at":"2020-09-03T13:29:27.000000Z", "user":{ "id":1, "name":"Shivrag Shukla", "email":"ines52@example.org", "email_verified_at":"2020-09-03T13:09:18.000000Z", "created_at":"2020-09-03T13:09:18.000000Z", "updated_at":"2020-09-03T13:09:18.000000Z" }, "comments":[ { "id":76, "content":"First Comment", "status":1, "created_at":"2020-09-03T18:37:31.000000Z", "updated_at":"2020-09-03T18:37:31.000000Z", "user":{ "id":2, "name":"Tanya Powlowski", "email":"ines52@example.org", "email_verified_at":"2020-09-03T13:09:18.000000Z", "created_at":"2020-09-03T13:09:18.000000Z", "updated_at":"2020-09-03T13:09:18.000000Z" }, "replies":[ { "id":3, "content":"First Reply", "status":1, "created_at":"2020-09-03T13:09:30.000000Z", "updated_at":"2020-09-03T13:09:30.000000Z", "user":{ "id":14, "name":"Mr. Zion Krajcik", "email":"damaris.stamm@example.org", "email_verified_at":"2020-09-03T13:09:19.000000Z", "created_at":"2020-09-03T13:09:19.000000Z", "updated_at":"2020-09-03T13:09:19.000000Z" } }, { "id":30, "content":"Another Reply", "status":1, "created_at":"2020-09-03T13:09:30.000000Z", "updated_at":"2020-09-03T13:09:30.000000Z", "user":{ "id":4, "name":"Mr. Shiva Shukla", "email":"damaris.stamm@example.org", "email_verified_at":"2020-09-03T13:09:19.000000Z", "created_at":"2020-09-03T13:09:19.000000Z", "updated_at":"2020-09-03T13:09:19.000000Z" } } ] }, { "id":77, "content":"Another comment", "status":1, "created_at":"2020-09-03T18:37:44.000000Z", "updated_at":"2020-09-03T18:37:44.000000Z", "user":{ "id":2, "name":"Tanya Powlowski", "email":"ines52@example.org", "email_verified_at":"2020-09-03T13:09:18.000000Z", "created_at":"2020-09-03T13:09:18.000000Z", "updated_at":"2020-09-03T13:09:18.000000Z" }, "replies":[ ] } ] }
开发支持者:Shivrag Shukla
如有疑问,请联系: shivragshukla001@gmail.com