3 回答
TA贡献1966条经验 获得超4个赞
如果你Post在控制器中有一个模型,你可以这样。
public function index()
{
$posts = Post::all();
return view('home')->with('posts', posts);
}
<ul>
@foreach($posts as $post)
<li>
<img src="{{ asset('storage') . '/' . $post->image }}" class="w-100 h-100">
</li>
@endforeach
</ul>
希望对你有帮助。
TA贡献1796条经验 获得超4个赞
只需在控制器中调用 post 模型即可
$posts = Post::select('columns_name')->get();
压缩变量,你可以像这样使用
@forelse($posts as $post)
{{$post->id}}
@empty
No post found
@endforelse
TA贡献1802条经验 获得超5个赞
如果您在 Post 模型中有这样的关系:
public function users()
{
return $this -> belongsToMany(User::class, 'user_posts');
}
//'user_posts' 是你的带有外键的数据库表
在控制器中使用:
$posts=Post::with('users')->get();
然后在视图中:
@foreach ($posts as $post)
<div class="col-4 mb-4">
<a href="{{ route('posts.show', ['post' => $post->id]) }}">
<img src="{{ asset('storage') . '/' . $post->image }}" class="w-100 h-100">
</a>
<a href="{{ route('users.show', ['user' => $post-> user -> id]) }}">
{{ $post-> user -> name }}
</a>
</div>
@endforeach
- 3 回答
- 0 关注
- 101 浏览
添加回答
举报