PostController
public function index()
{
$posts=Post::all();
return view('home')->with('posts', $posts);
}
web.php
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
Route::resource('posts','PostController');
home
@foreach($posts as $post)
<p>{{$post['content']}} </p>
@endforeach
我收到此错误
Facade \ Ignition \ Exceptions \ ViewException未定义的变量:帖子(查看:C:\ xampp \ htdocs \ lts \ resources \ views \ home.blade.php)$ posts是未定义的在刀片模板中将变量设为可选。将{{$ posts}}替换为{{$ posts ?? ”}}
show($ id)也不起作用,但是在控制器工作中创建并存储方法
尝试一下,而不是您给循环的东西
@foreach($posts as $post)
{{ $post->content}}
@endforeach
尝试替换
return view('home')->with('posts', $posts);
with
return view('home', ['posts' => $posts]);
和
<p>{{$post['content']}}</p>
与
<p>{{ optional($post)->content }}</p>
控制器中的Chnage
public function index()
{
$posts=Post::all();
return view('home', ['posts' => $posts]);
}
更换刀片
@foreach($posts as $post)
{{ $post->content}}
@endforeach