【问题标题】:how to display image from path stored in database? in laravel如何显示存储在数据库中的路径中的图像?在 laravel 中
【发布时间】:2018-05-09 01:42:56
【问题描述】:

我正在使用 laravel 开发一个网站,我正在尝试检索存储为数据库中路径的图像,但我的代码仅获取图像的名称和视图中的价格。我怀疑它是如何调用图像的,因为 laravel 已将它们存储在 storage/public 文件夹中。请帮忙。 这是我的代码: @section('内容')

<div class="container">


    @foreach ($products->chunk(4) as $items)
        <div class="row">
            @foreach ($items as $products)
                <div class="col-md-3">
                    <div class="thumbnail">
                        <div class="caption text-center">
                            <a href="{{ url('shop', [$products->slug]) }}"><img src="{{ URL::asset('public/' . $products->path) }}" alt="products" class="img-responsive"></a>
                            <h3>{{ $products->name }}</h3>
                            <div class="clearfix">
                            <div class="price pull-left"><p>{{ $products->price }}</p></div>
                            <a href="{{ url('shop', [$products->slug]) }}" class="btn btn-success pull-right" role="button">add to Cart</a>
                            </div>
                        </div> <!-- end caption -->
                    </div> <!-- end thumbnail -->
                </div> <!-- end col-md-3 -->
            @endforeach
        </div> <!-- end row -->
    @endforeach

</div> <!-- end container -->

然后在我的控制器中: if($request->hasFile('file')){ $uploaded_file = $request->file('file');

        // this get the original extention
        $uploaded_file_ex = $uploaded_file->getClientOriginalExtension();


        // the path to store the file
        // I add the time at the begining to avoid overwritting the file if another file has the same name.
        $filename = time().'.'.$uploaded_file_ex;
        $path = $uploaded_file->storeAs('public', $filename);

【问题讨论】:

  • 请出示您的代码
  • @Chalo 您的图片直接在公共文件夹中吗?如果您的图像没有固定路径,那么您也可以从控制器发送 $path in view。
  • 是的。在视图中发送 $path 是什么意思
  • $path 如果你为每张图片发送动态路径,你可以使用
  • 但如果您将所有图像仅存储在公用文件夹中并且具有修复路径,则不需要

标签: php


【解决方案1】:

您需要使用images 或其他一些您将存储图像的文件夹直接代替public。即使您想将图像存储在public 文件夹中,也无需在资产中提供public。默认情况下,它将从public 文件夹中选择,如果您使用图像的动态路径,则必须从控制器方法发送$path 以查看。

如果您直接从公共文件夹中获取图像,以下代码将为您提供帮助。

<div class="container">
@foreach ($products->chunk(4) as $items)
    <div class="row">
        @foreach ($items as $products)
            <div class="col-md-3">
                <div class="thumbnail">
                    <div class="caption text-center">
                        <a href="{{ url('shop', [$products->slug]) }}"><img 
src="{{ URL::asset($products->path) }}" alt="products" class="img-
responsive"></a>
                        <h3>{{ $products->name }}</h3>
                        <div class="clearfix">
                        <div class="price pull-left"><p>{{ $products->price 
}}</p></div>
                        <a href="{{ url('shop', [$products->slug]) }}" 
class="btn btn-success pull-right" role="button">add to Cart</a>
                        </div>
                    </div> <!-- end caption -->
                </div> <!-- end thumbnail -->
            </div> <!-- end col-md-3 -->
        @endforeach
    </div> <!-- end row -->
@endforeach

</div> <!-- end container -->

上传您需要在控制器方法中添加的图像脚本,您将从表单接收数据:-

// Upload Image Script
if($request->hasFile('image')){
    if (Input::file('image')->isValid()) {
         $file = Input::file('image');
         $destination = 'img/';
         $extension = Input::file('image')->getClientOriginalExtension();
         $fileName = rand(111,99999).'.'.$extension;
         $file->move($destination, $fileName);
     }
 }

【讨论】:

  • 哦,我知道你能帮我看看我如何从我的控制器发送 $path 来查看我有点迷路
  • 首先告诉我你在 $products->你在资产中用来获取图像的路径中得到了什么?
  • 我上传的图片,路径存储在db中,但图片存储在storage文件夹中,我从public/img文件夹中获取图片
  • 如果图像在 public/img 文件夹中,则更新为 src="{{asset('img/' . $products->path) }}"
  • 这是我从中检索然后通过数据库中的路径将它们存储在存储文件夹中的位置
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-27
  • 2014-12-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多