0

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);
7
  • Please show your code Commented Nov 25, 2017 at 7:12
  • @Chalo Your images in public folder directly? If your images is not having fix path then you can also send $path in view from controller. Commented Nov 25, 2017 at 7:23
  • Yes they are. what do you mean by sending the $path in view Commented Nov 25, 2017 at 7:30
  • $path you can use if you are sending dynamic path for every image Commented Nov 25, 2017 at 7:32
  • but if you are storing all images in public folder only and have fix path then no need Commented Nov 25, 2017 at 7:33

1 Answer 1

1

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);
     }
 }
Sign up to request clarification or add additional context in comments.

16 Comments

oh, I get could you please help me with how i send $path from my controller to view am kinda lost there
First tell me what are you getting in $products->path that you are using in asset to get image?
the images that i have uploaded, the path is stored in the db but the image is stored in the storage folder,, I am getting the images from public/img folder
if images are in public/img folder then update like src="{{ asset('img/' . $products->path) }}"
That is where i am retrieving from then storing them in the storage folder via the path in the database
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.