php - Laravel 7 : How to show single image on blade view when i have multiple images/path stored in database
Get the solution ↓↓↓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. 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
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
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 :)
Answer
Solution:
If you wanna show just one image you can use in 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
Answer
Solution:
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:
@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.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: trying to access array offset on value of type bool
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.