Php 如何显示特定帖子的评论?
我想在我的网站上显示对每项特定服务的评论。。 商店评论方法工作正常! 当我试图在blade页面中显示注释时,我得到了这个错误Php 如何显示特定帖子的评论?,php,laravel,laravel-5,Php,Laravel,Laravel 5,我想在我的网站上显示对每项特定服务的评论。。 商店评论方法工作正常! 当我试图在blade页面中显示注释时,我得到了这个错误 SQLSTATE[42S22]:未找到列:“where子句”中的1054未知列“comments.service_id”(SQL:select*fromcommentscommentsservice_id=11和commentsservice_id不为空)(视图:C:\xampp\htdocs\dawerezirou\resources\views\service\sho
SQLSTATE[42S22]:未找到列:“where子句”中的1054未知列“comments.service_id”(SQL:select*from
comments
commentsservice_id=11和comments
service_id不为空)(视图:C:\xampp\htdocs\dawerezirou\resources\views\service\showservice.blade.php)
CommentsController.php(存储评论)
这是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
@if(计数($services->comments)>0)
@foreach($services->comments as$comment)
$comment->user->username
$comment->body
@endforeach
@恩迪夫
Laravel有特定的命名约定-您的列应该是
服务id
,而不是服务id
。由于您没有使用该命名约定,因此需要告诉关系要在哪个列中查找:
return $this->hasMany('App\comment', 'services_id');
还要注意,在一些文件名区分大小写的操作系统上,
App\Comment
和App\Comment
是不同的。确保您使用了正确的大小写-App\Comment
、class Comment
和Comment.php
、或App\Comment
、class Comment
和Comment.php
。不是混搭 如果您不使用命名约定,则在创建laravel中的关系时始终添加适当的本地键和外键,因为laravel使用snakecase命名约定是的,对不起,我只是键入了错误的大写字母,我编辑了它。。。但我认为问题不在于“$services\u id”而在于“$services\u id”。@AtefRihane试试看。我很清楚这是你的问题。看到你在做什么了吗$comment->services\u id
,但是Laravel在做引用service\u id
的查询吗?我只是按照你告诉我的那样修改了,但是我仍然得到同样的错误:完全相同的错误?因为它还在抱怨单数服务id
列?或者类似?SQLSTATE[42S22]:未找到列:1054未知列“注释”。“where子句”中的“$services\u id”(SQL:select*fromcomments
wherecomments
$services\u id
不为空)
return $this->hasMany('App\comment', 'services_id');