2017-08-11 69 views
0

存储/应用程序/公开 - >这是我的图像存储路径。如何检索存储在laravel5.4存储文件夹中的图像?

控制器代码:

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

这是我存储的图像转换成laravel的存储路径的方式.The(即存储/应用程序/公共)。存储在这里成功完成。

查看代码:

@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 

在这里,我试图显示存储image.But没有显示在那里。

+0

尝试过'{{asset($ cdetail-> image)}}'? – linktoahref

+0

是的。但它只显示存储在公共文件夹 –

+0

中的图像对不起,我的坏!我有点无知 – linktoahref

回答

1

首先请确保您已将storage/app/public链接到public/storage

如果没有运行php artisan storage:link

然后在您的视图使用Storage::url('public/' . $cdetail->image)得到的链接。

+0

我已经运行了链接命令。它显示该目录已经存在。 –

+0

@nishanthi然后尝试使用资产(Storage :: url($ cdetail-> image))来获取图像。 – chanafdo

+0

对不起,它不显示 –

1

使用资产()函数像刀片模板

例如: {{ asset('storage/app/public/'.$cdetail->image) }}

在情况下,如果你想在你的控制器访问 asset('storage/app/public/'.$cdetail->image);

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

为什么使用$ imageName。我正在检索它从db –

+0

在控制器我应该在哪里做到这一点 - >(资产('存储/应用程序/公共/'。$ cdetail-> 图像);)更改 –

+0

是的,我只是指你会把你从你的数据库中获取的图像名称。 –

-1

我想你可以TR这可能是它的帮助:

<img src="{{ storage_path('app/public/$cdetail-> 
       image') }}" width="50" height="50"></img> 

希望为你工作!!!

+0

对不起,它不起作用 –

+0

在我的看法源页面看起来像这样 - > **​​ ** –

+0

@nishanthi好吧,让我检查一下 –

相关问题