Php 如何在注释中显示用户
我的应用程序中有一个评论框,它工作正常。但我想显示在我的帖子中发表评论的用户,我如何才能做到这一点?到目前为止,我有这个代码 这是我的看法Php 如何在注释中显示用户,php,laravel,Php,Laravel,我的应用程序中有一个评论框,它工作正常。但我想显示在我的帖子中发表评论的用户,我如何才能做到这一点?到目前为止,我有这个代码 这是我的看法 <div class="view-post"> <div class="body"> <h3>{{$posts->title}}</h3> <h6>{{$posts->created_at->toFormattedDa
<div class="view-post">
<div class="body">
<h3>{{$posts->title}}</h3>
<h6>{{$posts->created_at->toFormattedDateString()}}</h6>
<p><img src="{{ asset('img/' . $posts->image) }}" class="img-rounded"/></p>
<p>{{$posts->content}}</p>
</div>
<hr>
<section>
<h5>LEAVE US A COMMENT</h5>
<form action="{{ URL::route('createComment', array('id' => $posts->id))}}" method="post">
<div class="form-group">
<input class="form-control" type="text" placeholder="Comment..." name="content">
</div>
<input type="submit" class="btn btn-default" />
</form>
</section><br>
<section class="comments">
@foreach($posts->comment as $comments)
<blockquote>{{$comments->content}}</blockquote>
@endforeach
</section>
</div>
在模型中,可以在一个模型和另一个模型之间建立关系 像那样 用户模型
class User extends Model {
public function comments()
{
return $this->hasMany('App\Comment');
}
}
class Comment extends Model {
public function user()
{
return $this->belongsTo('App\User');
}
}
评论模式
class User extends Model {
public function comments()
{
return $this->hasMany('App\Comment');
}
}
class Comment extends Model {
public function user()
{
return $this->belongsTo('App\User');
}
}
因此,您可以使用
$comment = Comment::find(1);
$user = $comment->user()->get();
所以,除了用户与帖子和帖子与评论的关系之外,我还必须这样做?我想是的。像那样试一下。