php - View 找不到变量(laravel)

标签 php laravel

我的 Controller 中有以下操作:

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

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

最佳答案

如果你使用 [] 你可以这样写 ['currentPost' => $current_post]

全部

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

关于php - View 找不到变量(laravel),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48434623/

相关文章:

php - Laravel 使用 Eloquent 进行多对多选择

php - 如何使用 CodeIgniter 3.x 将 session 数据传递到 CKFinder?

php - 计算无限项目的百分比

javascript - 如何用php删除垃圾图标glyphicon

Laravel 无法从 Postman 获取数据

mysql - Laravel 在体育中的 Eloquent 关系

php - 如何在链接到一个主键的数据透视表上创建两个外键

javascript - 当我打电话时尝试多次重复 ng-repeat

php - 在 PHP 中基于位掩码获取数组值

javascript - 如何从 Laravel View 将数据发送到模态?