1

ページの post_id(post1) の下にあるすべてのコメントを取得したい:

//localhost/posts/post1/ 

表のコメント

id:int[primary],  comment:varchar,  post_id:varchar, comment_id:varchar
1                 this is a comment post_1           comment_1

テーブルポスト

id:int[primary], post_title:varchar, post_id:varchar
1                this is post title  post_1

モデルコメント.php

public function post()
{
    return $This->belongsTo('Post');

}

モデル post.php

public function comments()
    {
        return $this->hasMany('Comment');
    }

コントローラーの投稿Controller.php

 public function show($id)
    {
       $comments = Post::where('post_id','=',$id)->first()
                   ->comments()->where('post_id','=',$id)->get();

    }

//localhost/posts/post1/ にアクセスすると、関連するコメントが表示されません。SQL は次のように実行されます。

select * from `posts` where `post_id` = 'post1' limit 1
select * from `comments` where `comments`.`post_id` = '1' and `post_id` = 'post1'

`post_id= '1'`を削除して、対応するコメントを取得するにはどうすればよいですか?

4

1 に答える 1