hi,你好!欢迎访问本站!登录
本站由简数采集腾讯云宝塔系统阿里云强势驱动
当前位置:首页 - PHP框架 - Laravel - 正文 请牢记本站网址www.sosophp.cn

Laravel5完成嵌套批评的情势(代码详解)【laravel框架】,laravel,php

2019-12-02Laravel搜搜PHP网63°c
A+ A-
本篇文章给人人带来的内容是关于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

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="//cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div style="margin-top: 100px">
    <div class="col-md-10 col-md-offset-1">
        <h2>{{$post->title}}</h2>
        <h4>{{$post->content}}</h4>
        <hr>
        @include('comments.list',['collections'=>$comments['root']])
        <h3>留下您的批评</h3>
        @include('comments.form',['parentId'=>$post->id])
    </div>
</div>
</body>
</html>

comment.blade.php

<div class="col-md-12">
    <h5><span style="color:#31b0d5">{{$comment->owner->name}}</span>:</h5>
    <h5>{{$comment->body}}</h5>

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

    @if(isset($comments[$comment->id]))
        @include('comments.list',['collections'=>$comments[$comment->id]])
    @endif
    <hr>
</div>

form.blade.php

<form method="POST" action="{{url('post/'.$post->id.'/comments')}}" accept-charset="UTF-8">
    {{csrf_field()}}

    @if(isset($parentId))
        <input type="hidden" name="parent_id" value="{{$parentId}}">
    @endif

    <div class="form-group">
        <label for="body" class="control-label">Info:</label>
        <textarea id="body" name="body"  class="form-control" required="required"></textarea>
    </div>
    <button type="submit" class="btn btn-success">复兴</button>
</form>

list.blade.php

@foreach($collections as $comment)
    @include('comments.comment',['comment'=>$comment])
@endforeach

终究效果图以下

以上就是Laravel5完成嵌套批评的情势(代码详解)的细致内容,更多请关注ki4网别的相干文章!

  选择打赏方式
微信赞助

打赏

QQ钱包

打赏

支付宝赞助

打赏

  选择分享方式
  移步手机端
Laravel5完成嵌套批评的情势(代码详解)【laravel框架】,laravel,php

1、打开你手机的二维码扫描APP
2、扫描左则的二维码
3、点击扫描获得的网址
4、可以在手机端阅读此文章
标签: