簡體   English   中英

如何顯示對特定帖子的評論?

[英]How can I show comments for specific posts?

我想顯示我網站上每項特定服務的評論。商店評論方法正常運行! 當我嘗試在刀片頁面中顯示注釋時,出現此錯誤

SQLSTATE [42S22]:找不到列:1054“ where子句”中的未知列“ comments.service_id”(SQL:從comments中選擇*,其中comments service_id = 11並且comments service_id不為null)(視圖:C:\\ xampp \\ htdocs \\ dawerelzirou \\ resources \\ views \\ service \\ showservice.blade.php)

CommentsController.php(商店評論)

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

這是Comment.php

         class Comment extends Model{ 


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



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

                         }

這是Service.php

     class Service extends Model{




public function user(){

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

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

}
       }

刀片頁面:

@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

如果您不使用命名約定,則在laravel中建立關系時,請務必添加適當的本地和外鍵,因為laravel使用蛇形命名轉換

Laravel有特定的命名約定-您的列應該是service_id ,而不是services_id 由於您使用該命名約定,因此需要告訴該關系查找哪一列:

return $this->hasMany('App\comment', 'services_id');

還要注意,在某些文件名區分大小寫的操作系統上, App\\CommentApp\\comment是不同的。 確保使用正確的大小寫App\\Commentclass CommentComment.php App\\commentclass commentcomment.php 不要混!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM