douguai4653 2018-03-21 16:18
浏览 26
已采纳

如何显示特定帖子的评论?

I want to show comments for every specific service in my website.. the store comment method is working properly! and when I try to display comments in the blade page I get this error

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'comments.service_id' in 'where clause' (SQL: select * from comments where comments.service_id = 11 and comments.service_id is not null) (View: C:\xampp\htdocs\dawerelzirouesources\views\service\showservice.blade.php)

CommentsController.php (store comment)

public function store(Request $request,  $services_id)
{
   $this->validate($request,array(
        'body'=>'required',
    ));


  $comment=new Comment;

  $comment->body=$request->body;
  $comment->user_id=$request->user()->id;
  $comment->services_id=$services_id;
  $comment->save();



  $request->session()->flash('notif','success');
  return back();
}

this is Comment.php

         class Comment extends Model{ 


            public function  user() {
           return $this->belongsTo('App\User');}



             public function  services() {
             return $this->belongsTo('App\Service');}

                         }

This is Service.php

     class Service extends Model{




public function user(){

    return $this->belongsTo('App\User');
}

public function comments(){
    return $this->hasMany('App\comment');

}
       }

The blade page :

@if (count($services->comments)>0)

       @foreach ($services->comments as $comment)
            <div class="row">
            <div class="col s6 offset-l3">
            <div class="card small" style="height:auto;width:700px;">
            <div class="card-content center-align">
            <div class="row">
            <div class="col s12">
        <img src="/img/bg.jpeg" class="responsive-img circle center-align" 
        style="width:50%;" alt="">
        </div>
       <div class="row">
       <div class="col s12">
     <p>$comment->user->username </p>
       </div>
       </div>
        <br>
       <div class="row">
       <div class="col s12">
       <p>$comment->body</p>
           </div>
           </div>
           </div>
          </div>

          </div>
          </div>
          </div> 
       @endforeach
          @endif

  • 写回答

2条回答 默认 最新

  • doukang1962 2018-03-21 16:31
    关注

    Always add proper local and foreign keys while making the relations in laravel if you are not using naming conventions because laravel uses snakecase naming convrntions

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi