我正在使用laravel开发一个网站,我正在尝试检索存储为数据库中路径的图像,但我的代码只获取图像的名称和视图中的价格。我怀疑它是如何调用图像的,因为laravel已将它们存储在存储/公共文件夹中。请帮忙。 这是我的代码: @section('内容&#39)
<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 -->
然后在我的控制器中: 如果($请求 - &GT; hasFile(&#39;文件&#39;)){ $ uploaded_file = $ request-&gt;文件(&#39;文件&#39;);
// 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);
答案 0 :(得分:0)
您需要使用images
或其他文件夹来直接存储图片以代替public
。即使您想将图像存储在public
文件夹中,也无需在资产中提供public
。默认情况下,它会从public
文件夹中选择,如果您使用图像的动态路径,则必须从控制器方法发送$path
以进行查看。
如果直接从公共文件夹中获取图像,下面的代码将对您有所帮助。
<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
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);
}
}