视图在laravel中不起作用

时间:2018-05-03 14:27:14

标签: php html 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)

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

4 个答案:

答案 0 :(得分:0)

这样做,

public function index()
{
    $posts = Post::orderBy('id','desc')->paginate(1);
   return view('pages.blog',compact('posts'));
}

答案 1 :(得分:0)

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

@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

答案 2 :(得分:0)

除此之外:

  

使用->with()方法时,传递给它的第一个参数是   视图上可用变量的名称。所以评论   您可以在视图中进行此致电->with('commentShow',$show);   您尝试通过$post访问它。将此行@foreach($post as $commShow)更改为@foreach($commentShow as $commShow)

将您的查看代码更改为:(请注意第二个@foreach中更改的变量名称)

@if(isset($post) && !empty($post)) 
@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>
@endforeach
@endif

@if(isset($commentShow) && !empty($commentShow)) 
<!-- 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>
@endif

答案 3 :(得分:0)

在这里,您要将您的Eloquent模型集合定义为$commentShow

return view('pages/blog')->with('commentShow',$show);

在您看来,您使用的是变量$post。 您需要更改其中一个以匹配另一个。