【问题标题】:How to display multiple images from database in Laravel 8?如何在 Laravel 8 中显示数据库中的多个图像?
【发布时间】:2021-03-25 01:39:20
【问题描述】:

我被一些代码困住了。因此,当我尝试将图像加载到刀片文件上时,我现在将图像数据获取到数据库中,它没有找到图像,如果我将直接 url 放在地址栏中,它会显示 404 未找到。下面是代码。

控制器文件代码

public function displayReferenceImages($document_id){
    $is_ref_imgs_exist = Document::select('reference_images')->where('document_id', $document_id)->get();
    if(count($is_ref_imgs_exist)>0) {
        $reponse = $is_ref_imgs_exist[0]->reference_images;
        return view('testfile')->with(['sliders' => $reponse]);
    }else{
        return "No image found";
    }
}

所以响应是这样的:

public/HRTKD1607941923/HRTKDRI16079419230.jpg,public/HRTKD1607941923/HRTKDRI16079419231.jpg,public/HRTKD1607941923/HRTKDRI16079419242.jpg,

刀片文件代码:

@foreach (explode(',', $sliders) as $image)
<img src="{{ URL::to('storage/app/'.$image)}}">
@endforeach

现在不显示了。

在此之前,我使用代码来显示单个图像: 那就是:

public function displayImage($document_id)
{
    $is_cover_image_exist = Document::select('cover_image')->where('document_id', $document_id)->get();
    if(count($is_cover_image_exist)>0) {
        $temp_file_name = $is_cover_image_exist[0]->cover_image;
        $content = Storage::get($temp_file_name);
        $mime = Storage::mimeType($temp_file_name);
        $response = Response::make($content, 200);
        $response->header("Content-Type", $mime);
        return $response;
    }else{
        $content = Storage::get('public/default.jpg');
        $mime = Storage::mimeType('public/default.jpg');
        $response = Response::make($content, 200);
        $response->header("Content-Type", $mime);
        return $response;
    }
}

在刀片文件中:

<img src="{{ route('load_image',$suggestion->document_id) }}" class="img-fluid rounded" alt="">

我也试过了,但没有成功。

【问题讨论】:

  • 请提供所有图片所需的网址,图片的位置(是否在公共文件夹中)
  • 不,它在 storage/app/public 文件夹下
  • 尝试将其存储在public/assets/images这样的公共文件夹中,然后您可以直接访问它而无需转到控制器&lt;img src={{url('/assets/images/&lt;!--photo.type--&gt;')}} width="" height="" alt=""/&gt;的帮助
  • 只是一个信息。而不是这些方法,只需尝试使用 Laravel 媒体库插件。这对于这样的场景真的很有帮助。 spatie.be/docs/laravel-medialibrary/v9/introduction

标签: laravel laravel-8


【解决方案1】:
@foreach($images as $image)
        <?php foreach (json_decode($image->image_path) as $picture) { ?>
            <div class="col-lg-4 col-sm-12 col-md-6 mb-3">
                <img src="uploads/{{$picture}}" alt="{{$picture}}">
            </div>
        <?php } ?>
@endforeach

【讨论】:

    猜你喜欢
    • 2021-07-19
    • 2022-01-25
    • 2015-04-18
    • 2021-11-29
    • 2017-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多