Chalo
Chalo

Reputation: 9

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. I suspect its how i am calling the images as laravel has stored them in the storage/public folder. Please help. Here is my code: @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');

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

Upvotes: 0

Views: 5120

Answers (1)

Amit Gupta
Amit Gupta

Reputation: 2806

You need to use images or some other folder where you will store your images in place of public directly. Even if you want to store your images in public folder then no need to give public in asset. 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.

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

Upvotes: 1

Related Questions