Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

view can't find variable (laravel)

Tags:

php

laravel

I have the following action in my controller:

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

Then I have the following view:

 @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

now when I run this. I get the following error:

Undefined variable: currentPost

Can anyone tell me what I've done wrong?

like image 549
Marc Rasmussen Avatar asked Jan 20 '26 03:01

Marc Rasmussen


1 Answers

If you use [] you may write this ['currentPost' => $current_post]

All

public function viewPost($id)
{
    $current_post = forum_post::with('replies')->where('id', $id)->get();
    return view('forumViewPost',['currentPost' => $current_post]);
}
like image 144
Ilya Yaremchuk Avatar answered Jan 22 '26 14:01

Ilya Yaremchuk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!