首页 > 解决方案 > 我想得到帖子的评论,我得到未定义的变量错误

问题描述

我想显示帖子的评论,但它给了我未定义的变量错误

//这里是我的控制器

public function show($id){

    $comments=Comment::where('post_id',$id)->first();

    return view('user',compact('comments'));

}

//这里是我的评论表

public function up()
{
    Schema::create('comments', function (Blueprint $table) {
        $table->id();
        $table->text('description');
        $table->integer('commentable_id')->unsigned();
        $table->integer('post_id');
        $table->string('commentable_type');
        $table->timestamps();
    });
}

//这是我的刀片

<div class="post-footer-comment-wrapper" >
    <b><p>Şərhlər</p></b>
    @foreach($comments as $comment)
    <div class="comment-form">
        <section class="post-body">
            <p>{{  $comment->description  }}</p> <!--  burda ilisdim -->
        </section>
    </div>

***更新 *** 这是我的添加评论表

    <form method="post"   action="{{route('comments')}}">
                                @csrf
                                <br>
                                <br>
                            <label for="comment">Şərh yaz</label><br>&nbsp;
                            <textarea id="comment " name="comment" rows="3" cols="50">
                            </textarea>

                                <input type="hidden" value="{{$post->id}}" name="id">
                            <button type="submit" class="btn btn-primary">Şərh yaz</button>
                            </form>


                            <div class="media-body">
                                <a href="#" class="anchor-username"><h4 class="media-heading"> </h4></a>
                                <a href="#" class="anchor-time"> </a>
                            </div>
                        </div>

标签: phpcomments

解决方案


推荐阅读