php - 视图在 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)
任何帮助,将不胜感激。谢谢
解决方案
你的刀片看起来不错,没有错误,但让我们试试这个视图..
@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
推荐阅读
- html - 元素的边框底部在水平容器内被切断
- javascript - append().ready() 导致多个代码事件 | jQuery
- pine-script - 在 pine 脚本中计算 beta:我做错了什么?
- flutter - onTap 不断被呼叫
- scala - RDD[Double] 问题 - 尝试在 Spark Scala 中的 GroupBy 之后调用函数
- flutter - 如何从头开始创建我自己的自定义 RMTP 服务器并流式传输到多个目的地
- c# - rabbitmq频道应该留给消费者吗?
- javascript - “未定义”类型上不存在属性“句柄”-反应上下文和打字稿
- python - 函数恰好需要 2 个参数 3(给定)python cx_orcale 插入错误
- angular - 类型错误:“xxx”不是函数