【问题标题】:Show results of query in a different blade在不同的刀片中显示查询结果
【发布时间】:2019-05-08 15:53:50
【问题描述】:

最初我尝试在模式中显示结果,但将其更改为在不同的刀片中显示结果。我想使用 post_id 检索对帖子所做的所有 cmets。但我收到以下错误: 此路由不支持 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

【问题讨论】:

  • 你可以dd($comments)看看你是否在获取数据吗?
  • 对于您的观点,请尝试return View::make('commentboard')-&gt;with('comments', $comments);,对于您的路线:Route::get('/commentboard', 'CommentController@getComment')-&gt;name('commentboard');
  • @AmartyaBarua,您可以使用路由参数将 postid 从您的视图传递到您的控制器。
  • @AmartyaBarua 现在工作了吗?
  • 据我所知,确实不支持 get ,正如错误所述,您的路线发布到发布请求

标签: javascript jquery html laravel bootstrap-4


【解决方案1】:

基本上,这是我的方法:

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');
}

【讨论】:

    猜你喜欢
    • 2016-05-03
    • 1970-01-01
    • 2020-09-26
    • 2013-04-20
    • 2017-03-12
    • 2013-03-24
    • 1970-01-01
    • 1970-01-01
    • 2018-01-19
    相关资源
    最近更新 更多