0

Storage/app/public-->This is my storage path of an images.

Controller code:

public function addDeviceCategory(Request $cform)
{

if (Input::hasFile('imagefile'))
    {
    $name  = $this->getImageId().".".$cform->file('imagefile')-
 >getClientOriginalExtension();   

    $image = $cform->file('imagefile');        
    $resize = Image::make($image)->resize(700, 300)->encode($cform-
 >file('imagefile')->getClientOriginalExtension());
    $hash = md5($resize->__toString());

    $path = "public/" . $name;
    Storage::put($path, $resize->__toString());

    }
  $validation1=deviceCategory::select('deviceCategoryName')-
 >where("deviceCategoryName",$cform->deviceCategory)->first();

    if(is_null($validation1))
    {

    $temp=new deviceCategory;

    $temp->deviceCategoryId=$this->getDeviceCategoryId();
    $temp->deviceCategoryName=$cform->input('deviceCategory');
    $temp->subCategoryId=$cform->input('subCategory');
    $temp->image=$name;

    $temp->save();

    return redirect('formAddDeviceCategory');
   }

}

public function getImageId(){
  return md5(uniqid().microtime());
}

This is the way I'm storing the images into storage path of laravel(ie. Storage/app/public).The storage done successfully here.

View Code:

 @php 
            $l=1
            @endphp
            @foreach($cdetails as $cdetail)
            <tr>
              <td>{{$l++}}</td>
              <td>{{$cdetail->deviceCategoryId}}</td>
              <td>{{$cdetail->categoryName}}</td>
              <td>{{$cdetail->subCategoryName}}</td>
              <td>{{$cdetail->deviceCategoryName}}</td>

          **<td><img src="{{ asset('storage/app/public/$cdetail->
              image') }}"  width="50" height="50"></img></td>**
             @endforeach

Here I am trying to display the stored image.But nothing displays there.

3 Answers3

1

First make sure you have linked the storage/app/public to public/storage.

If not run php artisan storage:link

Then in your view use Storage::url('public/' . $cdetail->image) to get the link.

chanafdo
  • 5,016
  • 3
  • 29
  • 46
0

Use asset() function like

example in your blade template: {{ asset('storage/app/public/'.$cdetail->image ) }}

In case if you want to access in your controller asset('storage/app/public/'.$cdetail->image );

<td>
   <img src="{{ asset('storage/app/public/'.$cdetail->image) }}" width="50" 
    height="50"></img>
</td>
Saroj
  • 1,343
  • 4
  • 15
  • 31
  • why $imageName is used. I am retrieving it from db –  Aug 11 '17 at 07:01
  • In controller where should I need to do this -->(asset('storage/app/public/'.$cdetail-> image );) change –  Aug 11 '17 at 07:02
  • Yes, I just referring by that you will put your image name which you retrieving from your DB. – Saroj Aug 11 '17 at 07:02
  • If you want to access it, in any case, that's why I mentioned that you can also access by that in your controller – Saroj Aug 11 '17 at 07:03
  • Sorry, I don't know where to include this asset('storage/app/public/'.$cdetail->image ); –  Aug 11 '17 at 07:06
  • Did you got your result? – Saroj Aug 11 '17 at 07:14
  • I have made the changes, but didn't get the output. But in my view source page it appears like this-->http://localhost/AdminPanel/public/storage/app/public/iTunesArtwork.png. When I click on the click it shows NOt found Http Exception –  Aug 11 '17 at 08:15
  • Did you checked that your file name is same in the folder that you have in db. And try to use your System IP instead of localhost – Saroj Aug 11 '17 at 08:34
  • yes.I just renamed the file name and stored it in db –  Aug 11 '17 at 08:35
  • The link you gave me, it seems to be wrong because it should be like http://localhost/AdminPanel/storage/app/public/iTunesArtwork.png – Saroj Aug 11 '17 at 08:36
  • Nishanti come to chat session, we can discuss there https://chat.stackoverflow.com/rooms/151682/discussion-between-saroj-and-nishanthi – Saroj Aug 11 '17 at 08:37
-1

I think you can try this:

<img src="{{ storage_path('app/public/$cdetail->
              image') }}"  width="50" height="50"></img>
halfer
  • 19,824
  • 17
  • 99
  • 186
AddWeb Solution Pvt Ltd
  • 21,025
  • 5
  • 26
  • 57