我正在尝试使用Laravel 5.5创建一个博客,但我收到了这个错误。
"Missing required parameters for [Route: posts.show] [URI: posts/{post}]. (View: /home/vagrant/Code/dialhousesetup.test/resources/views/posts/index.blade.php)"
在我的postcontroller.php文件中。我这样做了所以我可以在我的数据库中输出所有帖子:
public function index()
{
//Create a variable and store all the blog posts in it from the data base
$posts = Post::all();
//Return a view and pass in the above variable
return view('posts.index', array('posts' => $posts));
}
这是我在/ posts页面上写的循环,用于显示所有博客文章。
<div class="events mb-100">
<div class="container">
<div class="row">
@foreach ($posts as $post)
<div class="col-md-6">
<a href="{{ route('posts.show'), $post->id }}">
<div class="single-event text-center">
<img src="http://www.dialhousehotel.com/wp-content/uploads/2018/01/CC_1419-The-Dial-House-website-01-18-04.jpg" alt="" style="border:2px solid #bb9b50;">
<h2>{{$post->title}}</h2>
<p>{{$post->main_body}}</p>
<div class="separator"></div>
<a href="{{ route('posts.show'), $post->id }}"><button type="submit" class="button button-simple mt-30">Read More</button></a>
</div>
</a>
</div>
@endforeach
</div>
</div>
</div>
老实说,我不知道自己做错了什么。
更新: 人们已经注意到存在语法问题,我用{{route('posts.show',$ post-&gt; id)}替换了{{route('posts.show'),$ post-&gt; id}} }
@foreach($posts as $post)
<div class="col-md-6">
<a href="{{ route('posts.show', $post->id) }}">
<div class="single-event text-center">
<img src="http://www.dialhousehotel.com/wp-content/uploads/2018/01/CC_1419-The-Dial-House-website-01-18-04.jpg" alt="" style="border:2px solid #bb9b50;">
<h2>{{$post->title}}</h2>
<p>{{$post->main_body}}</p>
<div class="separator"></div>
<a href="{{ route('posts.show', $post->id) }}"><button type="submit" class="button button-simple mt-30">Read More</button></a>
</div>
</a>
</div>
@endforeach
但是,我现在正在获得"Invalid argument supplied for foreach() (View: /home/vagrant/Code/dialhousesetup.test/resources/views/posts/index.blade.php)"
<?php $__currentLoopData = $posts; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $post): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
<div class="col-md-6">
<a href="<?php echo e(route('posts.show', $post->id)); ?>">
<div class="single-event text-center">
<img src="http://www.dialhousehotel.com/wp-content/uploads/2018/01/CC_1419-The-Dial-House-website-01-18-04.jpg" alt="" style="border:2px solid #bb9b50;">
<h2><?php echo e($post->title); ?></h2>
<p><?php echo e($post->main_body); ?></p>
<div class="separator"></div>
<a href="<?php echo e(route('posts.show', $post->id)); ?>"><button type="submit" class="button button-simple mt-30">Read More</button></a>
</div>
</a>
</div>
<?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
答案 0 :(得分:1)
您使用了错误的语法。将其更改为:
{{ route('posts.show', $post->id) }}
答案 1 :(得分:0)
错误就在这里。
<a href="{{ route('posts.show'), $post->id }}">
它需要ID作为路线的一部分。所以它会是
<a href="{{ route('posts.show', $post->id) }}">