在其他刀片中显示查询结果

时间:2019-05-08 15:53:50

标签: javascript jquery html laravel bootstrap-4

最初,我尝试以模式显示结果,但更改为在另一个刀片中显示结果。我想使用其post_id检索对帖子所做的所有评论。但是我收到以下错误: 该路由不支持GET方法。支持的方法:POST。

单击“所有评论”后从哪里获得我的post_id的刀片服务器:

<section class="row posts">
        <div class="col-md-6 col-md-3-offset">
            <header><h3>other posts</h3></header>
            @foreach($posts as $post)
            <article class="post">
                <p>{{ $post->content }}</p>
                <div class="info">Posted by {{ $post->user->username }} on {{ $post->created_at }}</div>
                <div class="interaction">
                    <a href="#" class="like" id="like" data-postid="{{ $post->id }}">{{ Auth::user()->likes()->where('post_id', $post->id)->first() ? 'Liked' : 'Like' }}</a> |
                    <a href="#" class="comment" id="comment" data-postid="{{ $post->id }}">Comment</a> |
                    <a href="{{ route('show.comments',['post_id' => $post->id]) }}" class="allcomments" id="allcomments" data-postid="{{ $post->id }}">All Comments</a>
                    @if(Auth::user() == $post->user)
                        |
                        <a href="#" class="edit" data-postid="{{ $post->id }}">Edit</a> |
                        <a href="{{ route('post.delete',['post_id' => $post->id]) }}">Delete</a>
                    @endif
                </div>
            </article>
            @endforeach
        </div>
    </section>

路线:

Route::middleware(['web'])->group(function()
{
Route::post('/showcomment/{post_id}',[
        'uses' => 'CommentController@getComments',
        'as' => 'show.comments',
        'middleware' => 'auth'
    ]);
}

控制器:

public function getComment($post_id)
    {
        $comments = Comment::where('post_id',$post_id)->orderBy('created_at','desc')->get();
        return view('showcomments',['comments' => $comments]);
    }

我要输出结果的刀片服务器

@extends('layouts.master')

@section('content')
    <section class="row posts">
        <div class="col-md-6 col-md-3-offset">
            <header><h3>other Comments</h3></header>
            @foreach($comments as $comment)
                <article class="comment">
                    <p>{{ $comment->body }}</p>
                    <div class="info">Made by {{ $comment->user->username }} on {{ $comment->created_at }}</div>
                    <div class="interaction">
                        @if(Auth::user() == $comment->user)
                            |
                            <a href="#" class="edit" data-commentid="{{ $comment->id }}">Edit</a> |
                            <!--copy delete from post here including the modal-->
                        @endif
                    </div>
                </article>
            @endforeach
        </div>
    </section>
@endsection

1 个答案:

答案 0 :(得分:0)

基本上,这是我的方法:

Route::get("/posts/{post_id}/comments", "PostsController@getComments");

控制器:

// get comments...
public function getComments ($postId) {
    $comments = Post::findOrFail($postId)->comments;

   return view('showcomments',['comments' => $comments];
}

要使以上方法起作用:在这种情况下,您应该已经在帖子模型中定义了帖子评论关系:

// Post Model: a post has many comments

public function comments () {
   return $this->hasMany(Comments::class, 'post_id');
}