[英]Laravel 7 : How to show single image on blade view when i have multiple images/path stored in database
I don't know my logic is wrong or I am doing something wrong.我不知道我的逻辑是错误的还是我做错了什么。
I have a page "all-business" where I am showing all businesses on a page.我有一个页面“所有业务”,我在页面上显示所有业务。 when someone clicks on a single business it will go to the "business-details" page.当有人点击单个业务时,它会 go 到“业务详细信息”页面。 Now what I want to do is showing single image on the all-business page.现在我想做的是在全业务页面上显示单个图像。 and when someone clicks on a single business than I want to show multiple images on the business-details page.当有人点击单个商家时,我想在商家详情页面上显示多张图片。 here is my code这是我的代码
Business Model商务 Model
class Business extends Model
{
protected $guarded = [''];
public function user()
{
return $this->belongsTo('App\User');
}
public function category()
{
return $this->belongsTo('App\Category');
}
public function subCategory()
{
return $this->belongsTo('App\SubCategory');
}
public function province()
{
return $this->belongsTo('App\Province');
}
public function city()
{
return $this->belongsTo('App\City');
}
}
controller controller
public function allBusinesses()
{
$businesses = Business::paginate(5);
return view('front-end.buy-business', compact('businesses'));
}
blade刀
<div class="col-md-9">
<div class="row">
@foreach ($businesses as $business)
{{-- {{dd($business)}} --}}
<div class="col-md-4">
<a href="" class="d-block text-reset box-anchor">
<div class="box-outer">
<div class="box-inner">
<img src="{{asset('storage/'.$business->image)}}" alt="" class="img-fluid" />
<hr />
<hr />
<h2 class="text-black-50">{{$business->price}} <span
class="text-small">Pkr</span></h2>
<p class="box-inner-p">{{$business->name}}</p>
<div class="box-bottom">
<span class="box-bottom-left">{{$business->user->name}}</span>
<span class="box-bottom-right">location</span>
</div>
</div>
</div>
</a>
</div>
@endforeach
</div>
</div>
if you want more details just comment thankyou:)如果您想了解更多详细信息,请发表评论谢谢:)
If you wanna show just one image you can use in foreach如果您只想显示一张图片,您可以在 foreach 中使用
@if ($loop->first)
This is the first iteration.
@endif
If you wanna just show show first image in all loop如果你只想在所有循环中显示第一个图像
$business[0]image[1]
You can use index您可以使用索引
Assuming you will edit your business model so it can actually have several images (like Berto99 said in his comment), what I would do then in the views is something like this:假设您将编辑您的业务 model 所以它实际上可以有几个图像(如 Berto99 在他的评论中所说),那么我会在视图中做的是这样的:
@forelse ($business->images as $image)
<img src="{{asset($image)}}" alt="" class="img-fluid" />
@break
@empty
{{-- If for some reason the business has no images, you can put here some kind of placeholder image, using 3rd party services or maybe your own generic image --}}
<img src="//via.placeholder.com/150x150" alt="" class="img-fluid" />
@endforelse
for the all-business view.对于全业务视图。 Then you can use almost the same block of code in your business-details view, you just need to get rid of the @break
sentence so it shows all images instead of only one.然后,您可以在业务详细信息视图中使用几乎相同的代码块,您只需要去掉@break
语句,以便它显示所有图像而不是只显示一个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.