我想显示我在index.blade.php的articles表中的一些数据。
所以我在ArticleController上添加了以下内容:
public function home()
{
$articles = Article::orderBy('id' , 'desc')->get();
return view('index');
}然后在刀片上,我添加了这个:
@foreach($articles as $article)
<div class="card-body">
<h2 class="card-title">{{ $articles->title }}</h2>
<p class="card-text">{{ $articles->body }}</p>
<a href="{{ $articles->slug }}" class="btn btn-primary">Read More →</a>
</div>
@endforeach但是现在我收到了这个错误消息:
Undefined variable: articles (View: index.blade.php)所以你介意帮我解决这个错误吗?我真的很感谢你们的任何想法或建议...
提前谢谢。
发布于 2021-02-07 15:45:18
您没有将文章发送到您的index.blade.php文件。您可以使用compact函数调用来发送关于index.blade.php文件的文章。
例如:
public function home()
{
$articles = Article::orderBy('id' , 'desc')->get();
return view('index');
}上面的函数应该是:
public function home()
{
$articles = Article::orderBy('id' , 'desc')->get();
return view('index',compact($articles));
}https://stackoverflow.com/questions/66085596
复制相似问题