如何获取用户博客下的图片
How can I get images under a blog for a user
我真正想要的是,对于特定用户,我试图在单个博客下显示每张图片。我得到的是一个博客 post 每个博客的图片。
控制器
$user_id = Session::get('id');
$user = Users::find($user_id);
$blogs = Blog::where('user_id', $user_id)->paginate(10);
$blogImage = BlogImage::where('blog_id', $blogs->pluck('id'))->get();
return view('Users.userlayout', compact('user', 'blogCat', 'blogs', 'username', 'blogImage'));
查看页面
@foreach($blogs as $blog)
<div class="post">
@foreach($blogImage as $img)
<img src="{{asset('storage/blog_img/'.$img->blog_img)}}" alt="Image"
class="img-responsive">
@endforeach
<p>
<?php $str = $blog->blog_desc; ?>
{{str_limit($str, 250, "...")}}
</p>
<a href="{{URL::to('/blog-details/'.$blog->id)}}" target="_blank" class="btn_1">
Read more
</a>
</div>
<hr>
@endforeach
这是因为您使用的是 where
而不是 whereIn
。
如果您尝试将数组或 collection 传递给 where
,它将仅使用第一个值。
$blogImage = BlogImage::whereIn('blog_id', $blogs->pluck('id'))->get();
因为这将 return 所有与 Blog
关联的 BlogImage
在分页列表中,我想您需要检查一下确保您只显示与特定 Blog
关联的图像。一种方法是使用 `@continue():
@foreach($blogImage as $img)
@continue($blogImage->blog_id !== $blog->id)
<img src="{{asset('storage/blog_img/'.$img->blog_img)}}" alt="Image" class="img-responsive">
@endforeach
综上所述,我建议在 Blog
和 BlogImage
之间使用 one-to-many relationship:
博客
public function images()
{
return $this->hasMany(BlogImage::class);
}
博客图片
public function blog()
{
return $this->belongTo(Blog::class);
}
然后在您的控制器中,您可以 Eager load 图像并具有类似的东西:
$blogs = Blog::with('images')->where('user_id', $user_id)->paginate(10);
您的 blade 文件将包含:
@foreach($blog->images as $image)
<img src="{{asset('storage/blog_img/'.$image->blog_img)}}" alt="Image" class="img-responsive">
@endforeach
然后您也可以在 User
和 Blog
之间应用相同的 one-to-many relationship 逻辑。
我真正想要的是,对于特定用户,我试图在单个博客下显示每张图片。我得到的是一个博客 post 每个博客的图片。
控制器
$user_id = Session::get('id');
$user = Users::find($user_id);
$blogs = Blog::where('user_id', $user_id)->paginate(10);
$blogImage = BlogImage::where('blog_id', $blogs->pluck('id'))->get();
return view('Users.userlayout', compact('user', 'blogCat', 'blogs', 'username', 'blogImage'));
查看页面
@foreach($blogs as $blog)
<div class="post">
@foreach($blogImage as $img)
<img src="{{asset('storage/blog_img/'.$img->blog_img)}}" alt="Image"
class="img-responsive">
@endforeach
<p>
<?php $str = $blog->blog_desc; ?>
{{str_limit($str, 250, "...")}}
</p>
<a href="{{URL::to('/blog-details/'.$blog->id)}}" target="_blank" class="btn_1">
Read more
</a>
</div>
<hr>
@endforeach
这是因为您使用的是 where
而不是 whereIn
。
如果您尝试将数组或 collection 传递给 where
,它将仅使用第一个值。
$blogImage = BlogImage::whereIn('blog_id', $blogs->pluck('id'))->get();
因为这将 return 所有与 Blog
关联的 BlogImage
在分页列表中,我想您需要检查一下确保您只显示与特定 Blog
关联的图像。一种方法是使用 `@continue():
@foreach($blogImage as $img)
@continue($blogImage->blog_id !== $blog->id)
<img src="{{asset('storage/blog_img/'.$img->blog_img)}}" alt="Image" class="img-responsive">
@endforeach
综上所述,我建议在 Blog
和 BlogImage
之间使用 one-to-many relationship:
博客
public function images()
{
return $this->hasMany(BlogImage::class);
}
博客图片
public function blog()
{
return $this->belongTo(Blog::class);
}
然后在您的控制器中,您可以 Eager load 图像并具有类似的东西:
$blogs = Blog::with('images')->where('user_id', $user_id)->paginate(10);
您的 blade 文件将包含:
@foreach($blog->images as $image)
<img src="{{asset('storage/blog_img/'.$image->blog_img)}}" alt="Image" class="img-responsive">
@endforeach
然后您也可以在 User
和 Blog
之间应用相同的 one-to-many relationship 逻辑。