视图找不到变量(laravel)

时间:2018-01-25 02:18:01

标签: php laravel

我的控制器中有以下操作:

public function viewPost($id)
{
    $current_post = forum_post::with('replies')->where('id', $id)->get();
    return view('forumViewPost',['currentPost', $current_post]);
}

然后我有以下观点:

 @extends('layout.app')
@section('content')
    <div class="container">

        <div class="nk-gap-2"></div>

        <ul class="nk-forum nk-forum-topic">
            <li>
                <div class="nk-forum-topic-author">
                    <img src="assets/images/avatar-2-sm.jpg" alt="Kurt Tucker">
                    <div class="nk-forum-topic-author-name" title="Kurt Tucker">
                        <a href="#">{{$currentPost->nickname}}</a>
                    </div>
                    <div class="nk-forum-topic-author-role">
                        Member
                    </div>
                </div>
                <div class="nk-forum-topic-content">
                    {{$currentPost->content}}
                </div>
                <div class="nk-forum-topic-footer">
              <span class="nk-forum-topic-date">June 19,
              2017</span> <span class="nk-forum-action-btn"><a href="#forum-reply" class="nk-anchor"> Reply</a></span>
                    <span class="nk-forum-action-btn"><a href="#"> Spam</a></span>
                    <span class="nk-forum-action-btn"><span class="nk-action-heart liked"><span class="num">1</span>
              Like</span></span>
                </div>
            </li>
            @foreach($currentPost->replies as $replies)

            @endforeach
        </ul>
    </div>
@endsection

现在我跑这个。我收到以下错误:

Undefined variable: currentPost

谁能告诉我我做错了什么?

2 个答案:

答案 0 :(得分:5)

如果您使用[],可以写下['currentPost' => $current_post]

所有

public function viewPost($id)
{
    $current_post = forum_post::with('replies')->where('id', $id)->get();
    return view('forumViewPost',['currentPost' => $current_post]);
}

答案 1 :(得分:0)

您也可以compact('');和Wreigh提到将$current_post更改为$currectPost

public function viewPost($id)
{
  $currentPost = forum_post::with('replies')->where('id', $id)->get();
  return view('forumViewPost', compact('currentPost'));
}
相关问题