简体   繁体   English

如何从存储在数据库中的路径显示图像? 在拉拉韦尔

[英]how to display image from path stored in database? in laravel

am developing a site using laravel and I am trying to retrieve images stored as a path in the database but my code only gets the name of the image and the price in the views. 我正在使用laravel开发站点,并且尝试检索存储为数据库中路径的图像,但是我的代码仅在视图中获取图像名称和价格。 I suspect its how i am calling the images as laravel has stored them in the storage/public folder. 我怀疑这是我如何调用图像,因为laravel已将其存储在storage / public文件夹中。 Please help. 请帮忙。 Here is my code: @section('content') 这是我的代码:@section('content')

<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 -->

Then in my controller: if($request->hasFile('file')){ $uploaded_file = $request->file('file'); 然后在我的控制器中: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);

You need to use images or some other folder where you will store your images in place of public directly. 您需要使用images或其他一些文件夹来直接将图像存储在public文件夹中。 Even if you want to store your images in public folder then no need to give public in asset. 即使你想存储在您的图像public文件夹,然后没有必要给public资产。 It will pick from public folder by default and if you are using dynamic path for images then you must send $path from your controller method to view. 它会从挑选public文件夹在默认情况下,如果你正在使用的图像动态路径,那么你必须把$path从你的控制器的方法来查看。

Below code will help you if you are directly getting images from public folder. 如果直接从公用文件夹获取图像,则下面的代码将为您提供帮助。

<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 that you need to add in your controller method where you are receiving your data from form :- 上传图像脚本,您需要将其添加到从表单接收数据的控制器方法中:-

// 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);
     }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM