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 關注
- 172 瀏覽
添加回答
舉報