因此,我正在Traversy Media上关注Laravel,他已经在YouTube上制作了教程,但是我尝试复制的代码以及他在视频中所做的操作存在问题,但是我仍然遇到一些错误>
这是我遇到的错误
未定义变量:标题(视图:C:\ xampp \ htdocs \ Projects \ centralsocial2 \ resources \ views \ posts \ index.blade.php)
--- PostsController.php ---
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$posts = Post::all();
return view('posts.index')->with('posts' , $posts);
}
im将帖子扩展到此
@extends ('layouts.app')
@section('content')
<h1> Posts </h1>
@if(count($posts) > 1)
@foreach($posts as $post)
<h3> {{$post->$title}}</h3>
<p> {{$post->$body}} </p>
@endforeach
@else
<p> no posts found </p>
@endif
@endsection
他在视频中使用的Laravel是否过时?预先谢谢你们!
答案 0 :(得分:5)
在视图文件中,您必须在$ post变量循环后删除$,如下所示:
@extends ('layouts.app')
@section('content')
<h1> Posts </h1>
@if(count($posts) > 1)
@foreach($posts as $post)
<h3> {{$post->title}}</h3>
<p> {{$post->body}} </p>
@endforeach
@else
<p> no posts found </p>
@endif
@endsection