未定义变量评论[关闭]

问题描述 投票:-1回答:1

Facade \ Ignition \ Exceptions \ ViewException未定义的变量:评论(视图:C:\ Documents \ xamppfyp \ htdocs \ FYP \ resources \ views \ posts \ show.blade.php)http://fyp.test/posts/1隐藏解决方案$ reviews未定义在刀片模板中将变量设为可选。将{{$ reviews}}替换为{{$ reviews ?? ''}}C:\ Documents \ xamppfyp \ htdocs \ FYP \ resources \ views / posts / show.blade.php:85

使变量为可选

我的控制器如下:

use Illuminate\Http\Request;
use App\Review;
use App\Post;

class ReviewsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {

        $reviews = Review::all();
        return view('posts.show')->with('reviews', $reviews);

    }`

我的看法如下:`

<div class= 'Info'>
<br>
<b> 
- {{$post->Distance}}
<br>
- {{$post->Bedrooms}}
<br>
- {{$post->Price}}</b>
<br>
</div>
<div class = 'reviews'>
<h1>Reviews</h1>
@if(count($reviews) > 1)
    @foreach($reviews as $review)
        <div class= "well">
            <h3><a href="/reviews/{{$review->id}}">{{$review->title}} </a></h3><small>{{$review->created_at}}</small><br>
            <small>{{$review->body}}</small> 

        </div>
        <br>
        <br>
        @endforeach

        @else
    </p>no posts found</p>
    @endif
    <button class="button1" button onclick="location.href='{{ url('/Create') }}'" type="button">Write a review</button>

</div>
<button class="button1" button onclick="location.href='{{ url('/reviews') }}'" type="button">Reviews</button>

我的帖子控制器:

use App\Review;

class PostsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $posts = Post::all();
        return view('posts.index')->with('posts', $posts);

    }

My posts controller contains this which returns the same view:

    public function show($id)
    {
        $post = Post::find($id);
        return view('posts.show')->with('post', $post);


    }

laravel variables view controller
1个回答
0
投票

检查您的PostController。您没有将$ reviews变量传递给视图

public function index()
{
    $posts = Post::all();
    $reviews = Review::all();
    return view('posts.index')->with(compact('posts','reviews'));
}

-1
投票

更改此行

<button class="button1" button onclick="testfunction();" type="button">Reviews</button>

将此代码放入Javascript中

function testFunction () {
    location.href='{{ url('reviews') }}'    
}
© www.soinside.com 2019 - 2024. All rights reserved.