简体   繁体   English

Laravel 7:当我有多个图像/路径存储在数据库中时,如何在刀片视图上显示单个图像

[英]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.

相关问题 如何通过Laravel 5.3从数据库显示多个图像到刀片视图? - how to display multiple images from database to blade view, by Laravel 5.3? 如何在多个复选框中显示选中未选中 laravel 编辑刀片视图 - how to show checked unchecked in multiple checkbox laravel edit blade view 如何使用Laravel中的刀片从数据库检索图像并将其显示在视图上 - How to retrieve image from database and display it on view using blade in laravel 如何将多个图像路径存储在一个数组中以将值存储在数据库中的一行中,以便为php上传多个图像 - how to store multiple image path in one array to store in database the value in a single row for multiple images uploads for php Laravel如何将图像上传到数据库并在视图中显示 - Laravel how to upload an image to a database and show it in view 如何在 Laravel Blade 中用 html 显示单个图像? - How to display a single image with html in Laravel Blade? 如何在 Laravel 中将多个图像提取到刀片中 - How to fetch multiple Images into blade in Laravel 如何使用 VUE 在 Laravel Blade 中显示数据库信息 - How to show database info in Laravel Blade with VUE 如何在 Laravel 刀片模板中显示上传的图像? - How to show uploaded image in Laravel blade template? 我在数组中有数据,必须在 Laravel 刀片模板中显示,但在尝试获取名称时显示错误 - I have data in array and have to show in Laravel blade template but its showing error when try to get name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM