我有一个错误..它说:
未定义的变量:posts(查看:C:\xampp\htdocs\dinsospermasdes\bansus\resources\views\admin\posts\index.blade.php)
它在 home.blade.php 中的工作方式如下:
@foreach ($posts as $post)
<div class="card mb-4">
<img class="card-img-top" src="{{filter_var($post->post_image, FILTER_VALIDATE_URL) ? $post->post_image : '/storage/' . $post->post_image}}" alt="Card image cap">
<div class="card-body">
<h2 class="card-title">{{$post->title}}</h2>
<p class="card-text">{{Str::limit($post->body, '50', '....')}}</p>
<a href="{{route('post', $post->id)}}" class="btn btn-primary">Read More →</a>
</div>
<div class="card-footer text-muted">
Posted on {{$post->created_at->diffForHumans()}} by
<a href="#">{{$post->name}}</a>
</div>
</div>
@endforeach
但在 admin/posts/index.blade.php 中不起作用,如下所示:
@foreach($posts as $post)
<tr>
<td>{{$post->id}}</td>
<td>{{$post->title}}</td>
<td>
<img height="40px" src="{{filter_var($post->post_image, FILTER_VALIDATE_URL) ? $post->post_image : '/storage/' . $post->post_image}}">
</td>
<td>{{$post->created_at->diffForHumans()}}</td>
<td>{{$post->updated_at->diffForHumans()}}</td>
</tr>
@endforeach
这是 PostController.php:
class PostController extends Controller
{
//
public function index(){
$post = Post::all();
return view('admin.posts.index');
}
public function show(Post $post){
return view ('blog-post', ['post'=>$post]);
}
public function create(){
return view ('admin.posts.create');
}
鸿蒙传说