Upload images and showing them in a view Laravel 5.2

I made a function in which I upload images of some products. When I iterate through all my products, I want to show the image too.

The problem is, the image is being uploaded, but I can’t present it when i iterate with @foreach. I tried in the controller to get the path but it didn’t work.

So far I have done this:

In config/filesystems.php I created a custom storage

'products' => [
        'driver' => 'local',
        'root'   => public_path('/pictures/uploads/products'),
    ],

When I create a new product:
<h1>Create new product</h1>
  {!! Form::open(['route' => 'vendor.allproducts', 'files'=>true]) !!}
  <div class="form-group">
     {!! Form::label('Title', 'Title:') !!}
     {!! Form::text('title',null,['class'=>'form-control']) !!}
  </div>
  <div class="form-group">
     {!! Form::label('File', 'File:') !!}
     {!! Form::file('image') !!}
  </div>

When i iterate through my products info:
@foreach ($products as $product)
    <tr> 
      .
      .

       <td><img src="{{ $product->imagePath }}"></td>
     </tr>
 @endforeach

And in Controller:
public function store(){
    $product = Request::all();
    $file = Request::file('image');
    $extension = $file->getClientOriginalExtension();
    Storage::disk('products')->put($file->getFilename().'.'.$extension,  File::get($file));
    //$product->imagePath = Input::file('image')->getRealPath();
    Auth::user()->products()->create($product);
    return redirect()->route('allproducts');
}

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Your Store method will be

if(input::hasfile("image")){
        $files = Input::file('image');
        $name = time()."_". $files->getClientOriginalName();
        $image = $files->move(public_path().'/image' , $name);
        $imagefile->image=$name;
    }
         $imagefile->save();

And your view should be as
<td><img src="{{URL::to('/image/' . $product->image)}}"></td>


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x