首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法显示外键值

问无法显示外键值
EN

Stack Overflow用户
提问于 2019-05-30 03:15:21
回答 2查看 80关注 0票数 1

我试图创建一个帖子和评论部分,我设法在一个页面中显示所有的帖子,现在我试图在每个帖子下面显示评论。在我的模型中,我连接了两个表,在我的函数中,我能够在使用dd();时查看帖子和注释--这是我的结果:

​

​

我不确定我是否在我的视图中正确地调用了评论部分,因为我得到了错误:

属性activity_post_comments不存在于此集合实例上。(浏览: C:\xampp\xampp\htdocs\rsapp\resources\views\pages\timeline.blade.php)

我的职能:

代码语言:javascript
复制
public function timeline()
{
    $activityposts = ActivityPost::with('activity_post_comment')
                                ->orderBy('created_at','desc')
                                ->paginate(100);

    $posts = Post::orderBy('created_at','desc')->paginate(100);

    // dd($activityposts);

    return view('pages.timeline')
            ->with('posts', $posts)
            ->with('activityposts', $activityposts);
}

查看:

代码语言:javascript
复制
@if(count($activityposts) > 0)
    @foreach($activityposts as $activitypost)
    <div class="panel panel-default">
        <!--
        <div class="panel-heading"><a href="#" class="pull-right">View all</a>
        <h4>Bootply Editor &amp; Code Library</h4></div>
        -->
        <div class="panel-body">
            <p>
                <img src="../storage/user_image/{{ $activitypost->user->user_image }}" width="28px" height="28px">
                <b>{{ $activitypost->user->firstname }} {{ $activitypost->user->lastname }}</b>
                <span class="pull-right">{{ $activitypost->created_at->format('d-m-Y') }}</span>
            </p>
            <!--
                <div class="clearfix"></div>
                <hr>
            -->
            {{ $activitypost->status_update }}
            <hr>
            <div class="well"> 
                {{ $activitypost->activity_post_comments->activity_post_comments }};
            </div>
            <hr>
            <form class="form-horizontal" role="form" method="POST" enctype="multipart/form-data" action="{{ action('PagesController@storecomments', $activitypost->id) }}">
                {{ csrf_field() }}
                <div class="form-group{{ $errors->has('activity_post_comments') ? ' has-error' : '' }}">
                    <!-- <label for="activity_post_comments" class="col-md-4 control-label">First Name</label> -->
                    <div class="col-md-12">
                        <input id="activity_post_comments" type="text" class="form-control" name="activity_post_comments" required autofocus>
                        @if ($errors->has('activity_post_comments'))
                        <span class="help-block">
                        <strong>{{ $errors->first('activity_post_comments') }}</strong>
                        </span>
                        @endif
                    </div>
                </div>
                <button type="submit" class="btn btn-primary">Post</button>
            </form>
        </div>
    </div>
    @endforeach
{{$activityposts->links()}}
@else
<p>No new activity from users</p>
@endif

在这里我不确定:

代码语言:javascript
复制
{{ $activitypost->activity_post_comment->activity_post_comments }};
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-05-30 03:47:28

由于activity_post_comment将是一个集合,因此需要遍历相同的集合才能获得单独的实例。

代码语言:javascript
复制
@foreach ($activitypost->activity_post_comment as $comment)
<p>{{ $comment->activity_post_comments }}</p>
@endforeach
票数 2
EN

Stack Overflow用户

发布于 2019-05-30 05:06:00

正如linktoahref所指出的,你不确定的事情是正确的。$activitypost和$activity_post_comment之间的关系是1-n,或者$activitypost将有一个$activity_post_comment集合,所以在使用集合时,可以使用for-循环或其他方便的助手方法。为循环可能是

代码语言:javascript
复制
@foreach ($activitypost->activity_post_comment as $comment)
<p>{{ $comment->activity_post_comments }}</p>
@endforeach

或者如果您只想要一个activity_post_comments数组

代码语言:javascript
复制
$activitypost->activity_post_comment->pluck('activity_post_comments')
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56371171

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档