登录  /  注册
首页 > php框架 > Laravel > 正文

Laravel5实现嵌套评论的形式(代码详解)

不言
发布: 2018-12-21 09:52:14
转载
3250人浏览过

本篇文章给大家带来的内容是关于laravel5实现嵌套评论的形式(代码详解),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

经常我们看见评论显示形式有很多,比如'@'某某,又或者像知乎的收缩式的评论,又或者是嵌套式的评论,那么最一开始也是最常见的就是嵌套式评论,因为这个更加醒目.

准备工作

1、设计三张表users,posts,comments,表结构如下:

users

Schema::create('users', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->string('email')->unique();
    $table->string('password');
    $table->rememberToken();
    $table->timestamps();
});
登录后复制

posts

Schema::create('posts', function (Blueprint $table) {
    $table->increments('id');
    $table->string('title');
    $table->integer('user_id')->index();
    $table->text('content');
    $table->timestamps();
});
登录后复制

comments

Schema::create('comments', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('user_id')->index();
    $table->integer('post_id')->index();
    $table->integer('parent_id')->index()->default(0);
    $table->text('body');
    $table->timestamps();
});
登录后复制

2.Model层:
Post.php文件

/**
 * 一篇文章有多个评论
 * @return \Illuminate\Database\Eloquent\Relations\HasMany
 */
public function comments()
{
    return $this->hasMany(Comment::class);
}

/**
 * 获取这篇文章的评论以parent_id来分组
 * @return static
 */
public function getComments()
{
    return $this->comments()->with('owner')->get()->groupBy('parent_id');
}
登录后复制

Comments.php文件

/**
 * 这个评论的所属用户
 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
 */
public function owner()
{
    return $this->belongsTo(User::class, 'user_id');
}

/**
 * 这个评论的子评论
 * @return \Illuminate\Database\Eloquent\Relations\HasMany
 */
public function replies()
{
    return $this->hasMany(Comment::class, 'parent_id');
}
登录后复制

逻辑编写
我们所要实现的嵌套评论其实在我们准备工作中已经 有点思路了,我们首先将一篇文章显示出来,同时利用文章与评论的一对多关系,进行显示所有的评论,但是我们的评论里面涉及到一个字段就是parent_id,这个字段其实非常的特殊,我们利用这个字段来进行分组, 代码就是上面的return $this->comments()->with('owner')->get()->groupBy('parent_id'),具体的过程如下:

web.php文件

\Auth::loginUsingId(1); //用户id为1的登录

//显示文章和相应的评论
Route::get('/post/show/{post}', function (\App\Post $post) {
    $post->load('comments.owner');
    $comments = $post->getComments();
    $comments['root'] = $comments[''];
    unset($comments['']);
    return view('posts.show', compact('post', 'comments'));
});

//用户进行评论
Route::post('post/{post}/comments', function (\App\Post $post) {
    $post->comments()->create([
        'body' => request('body'),
        'user_id' => \Auth::id(),
        'parent_id' => request('parent_id', null),
    ]);
    return back();
});
登录后复制

视图代码
视图方面我们需要实现嵌套,那么随着用户互相评论的越来越多的话,那么嵌套的层级也就越多,所以说,我们这里需要使用各小技巧来显示整个评论,我们使用@include()函数来显示,那么我们试图的结构如下:

 - comments
comments.blade.php
form.blade.php
list.blade.php

 - posts
show.blade.php
登录后复制

代码如下:
show.blade.php

nbsp;html>


    <meta>
    <meta>
    <meta>
    <link><div>
    <div>
        <h2>{{$post-&gt;title}}</h2>
        <h4>{{$post-&gt;content}}</h4>
        <hr>
        @include('comments.list',['collections'=&gt;$comments['root']])
        <h3>留下您的评论</h3>
        @include('comments.form',['parentId'=&gt;$post-&gt;id])
    </div>
</div>

登录后复制

comment.blade.php

<div>
    <h5>
<span>{{$comment-&gt;owner-&gt;name}}</span>:</h5>
    <h5>{{$comment-&gt;body}}</h5>

    @include('comments.form',['parentId'=&gt;$comment-&gt;id])

    @if(isset($comments[$comment-&gt;id]))
        @include('comments.list',['collections'=&gt;$comments[$comment-&gt;id]])
    @endif
    <hr>
</div>
登录后复制

form.blade.php

登录后复制
id.'/comments')}}" accept-charset="UTF-8">     {{csrf_field()}}     @if(isset($parentId))              @endif     
                      
    

list.blade.php

@foreach($collections as $comment)
    @include('comments.comment',['comment'=&gt;$comment])
@endforeach
登录后复制

最终效果图如下

3065081519-5c1b8b4ace9e2_articlex.gif

以上就是Laravel5实现嵌套评论的形式(代码详解)的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
相关标签:
来源:segmentfault网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号