Laravel 5.2-显示数据库中的第一个图像 [英] Laravel 5.2 - show first image from database
本文介绍了Laravel 5.2-显示数据库中的第一个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的数据库中有存储图像的表photos
.在一个视图中,用户可以看到所有图像,但在另一个视图中,我只希望看到一个图像,第一个.如何设置它以便仅显示第一张图像,而不是全部显示?
I have table photos
in my database where my images are stored. In one view user can see all of them, but in the other I want only one image to be seen, first one. How to set it so only the first image is displayed, not all of them?
这是我的代码.
查看
@foreach($business->photos as $photo)
<img src="{{ url($photo->thumbnail_path) }}" class="img-thumbnail">
@endforeach
ProfileController
public function profile($id)
{
$user = User::findOrFail($id);
return view('business.profile', compact('user'));
}
Photo.php
public function business()
{
return $this->belongsTo('App\Business');
}
public function baseDir()
{
return 'images/';
}
public function setNameAttribute($name)
{
$this->attributes['name'] = $name;
$this->path = $this->baseDir() . '/' . $name;
$this->thumbnail_path = $this->baseDir() . '/tn' . $name;
}
PhotosController
public function store($id, AddPhotoRequest $request)
{
$business = Business::fidOrFail($id);
$photo = $request->file('photo');
(new AddPhotoToBusiness($business, $photo))->save();
}
推荐答案
解决方案:
@if(count($business->photos))
<img src="{{ url($business->photos[0]->thumbnail_path) }}" class="img-thumbnail" alt="">
@else
<img src="{{ URL::asset('/img/no-image.png') }}" class="img-responsive" alt="">
@endif
这篇关于Laravel 5.2-显示数据库中的第一个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文