首页 > 解决方案 > 视图在 laravel 中不起作用

问题描述

我是 laravel 的新手,正在做我的第一个名为 blog 的项目。对于帖子文章,我可以获取数据,但我试图通过从数据库中获取来在文章下方的索引页面中显示读者的评论,但它给出了错误(对于信息,我已经插入了一行xammp来获取)。这里是代码PostController.php

public function index()
{
    //$show = Post::all();
    $show = Post::orderBy('id','desc')->paginate(1);
   return view('pages/blog')->with('post',$show);
}


public function comment()
{
    $show = readerComment::all();
    return view('pages/blog')->with('commentShow',$show);
}

在索引页面或 blog.blade.php 中

@if(count($post)>0)
                            @foreach($post as $article)
                                <div class = "row">
                                    <div class="col-md-12">
                                        <h3 class="text-center">{{$article->title}}</h2>
                                        </div>
                                    </div>
                                <div class="row">
                                    <div class="col-md-12">
                                        <p>{!!$article->article!!}</p>
                                    </div>
                                </div>

                            <!-- Comment section -->
                            <div class = "comment">
                                <h3>Comments</h3>

                                @foreach($commentShow as $commShow)
                                    <div class = "row">
                                        <div class = "col-md-12">
                                        <p>{{$commShow->comment}}</p>
                                        <p>{{$commShow->name}}</p>
                                        </div>
                                    </div>
                                @endforeach

                            </div>

在网络路由中

 Route::resource('posts','PostController');
 Route::get('/','PostController@comment');

我得到错误

Undefined variable: post (View: C:\xampp\htdocs\blogging\resources\views\pages\blog.blade.php)

任何帮助,将不胜感激。谢谢

标签: phphtmllaravel

解决方案


你的刀片看起来不错,没有错误,但让我们试试这个视图..

@extends('layouts.app')
@section('content')
                    @if(count($post)>0)
                        @foreach($post as $article)
                            <div class = "row">
                                <div class="col-md-12">
                                    <h3 class="text-center">{{$article->title}}</h2>
                                    </div>
                                </div>
                            <div class="row">
                                <div class="col-md-12">
                                    <p>{!!$article->article!!}</p>
                                </div>
                            </div>

                        <!-- Comment section -->
                        <div class = "comment">
                            <h3>Comments</h3>

                            @foreach($post as $commShow)
                                <div class = "row">
                                    <div class = "col-md-12">
                                    <p>{{$commShow->comment}}</p>
                                    <p>{{$commShow->name}}</p>
                                    </div>
                                </div>
                            @endforeach

                    </div>@endsection

推荐阅读