Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在注释中显示用户_Php_Laravel - Fatal编程技术网

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

所以,除了用户与帖子和帖子与评论的关系之外,我还必须这样做?我想是的。像那样试一下。